找到了与请求匹配的多个操作但是设置了WebApiConfig

时间:2018-02-01 06:54:40

标签: c# asp.net-web-api

我正在使用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;
        }

有什么问题?请帮忙。非常感谢你

1 个答案:

答案 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")]

请参阅herehere有关属性路由的更多信息