我正在使用asp.net web api。我的问题是“找到了匹配请求的多个操作”但我已经设置了路由模板,并且在我的控制器中我有2个POST操作
**WebApiConfig**
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
这是我的控制器
1. [Route("{InsertRoadMap}")]
[HttpPost]
public mdRoadMapCallBack InsertRoadMap(mdEvent _mdEvent){
mdRoadMapCallBack _mdRoadMapCallBack = new mdRoadMapCallBack();
return _mdRoadMapCallBack;
}
2. [Route("{UpdateRoadMap}")]
[HttpPost]
public mdRoadMapCallBack UpdateRoadMap(mdEvent _mdEvent)
{
mdRoadMapCallBack _mdRoadMapCallBack = new mdRoadMapCallBack();
return _mdRoadMapCallBack;
}
有什么问题?请帮忙。非常感谢你
答案 0 :(得分:0)
似乎没有正确实现Route
属性。
改为使用它 -
[Route("api/mycontroller/InsertRoadMap")]
[Route("api/mycontroller/UpdateRoadMap")]
现在您可以像这样浏览 - http://server/api/mycontroller/InsertRoadMap
您还可以在控制器级别使用RoutePrefix
属性,这样您就不必为每种方法重复api
。
[RoutePrefix("api")]
然后你可以设置像 -
这样的路线[Route("mycontroller/InsertRoadMap")]
[Route("mycontroller/UpdateRoadMap")]