更新时间:2022-10-30 09:29:32
如果你不要求必然要使用REST服务,使用 API / {控制器} / {ID}
路线和尝试的基础上的方法来解决行动获取 / 发表 / 删除 / 把,您可以修改你的路线,经典的MVC路线 API / {控制器} / {行动} / {ID}
,它会解决你的问题。
I am using web API and i am new in this. I am stuck in a routing problem. I have a controller with following actions :
// GET api/Ceremony
public IEnumerable<Ceremony> GetCeremonies()
{
return db.Ceremonies.AsEnumerable();
}
// GET api/Ceremony/5
public Ceremony GetCeremony(int id)
{
Ceremony ceremony = db.Ceremonies.Find(id);
return ceremony;
}
public IEnumerable<Ceremony> GetFilteredCeremonies(Search filter)
{
return filter.Ceremonies();
}
The problem occure when i added the action GetFilteredCeremonies
to my controller. After adding this when i make an ajax call to GetCeremonies
action then it return an Exception with following message :
"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were
found that match the request
FYI: The parameter Search
is the Model class which contains properties and a function name Ceremonies.
EDIT
Route:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
If you're not requirement bound to use REST services that use api/{controller}/{id}
route and attempt to resolve action based on method GET/POST/DELETE/PUT, you can modify your route to classic MVC route to api/{controller}/{action}/{id}
and it will solve your problems.