【问题标题】:attribute routes on multiple controllers match the requested url多个控制器上的属性路由匹配请求的 url
【发布时间】:2017-03-27 08:24:00
【问题描述】:

我正在使用 web api 项目,其中我使用了两个控制器:

第一个控制器如下:

public class SmartlingController : BaseApiController
{
    [Route("api/smartling/ProcessSmartlingTranslation")]
    [VersionedRoute("", 1)]
    [ResponseType(typeof(HttpResponseMessage))]
    [HttpPost]
    public IHttpActionResult ProcessSmartlingTranslation(HttpRequestMessage request)
    {
       //some business logic
    }
}

第二控制器:

public class CommentsController : BaseApiController
{
    [Route("api/comments/GetAndPostBlogComments")]
    [VersionedRoute("", 1)]
    [ResponseType(typeof(HttpResponseMessage))]
    [HttpPost]
    public IHttpActionResult GetAndPostBlogComments([FromBody] BlogAndStoryComment comment)
    {
       //some business logic
    }
    [Route("api/comments/GetAndPostStoryComments")]
    [VersionedRoute("", 1)]
    [ResponseType(typeof(HttpResponseMessage))]
    [HttpPost]
    public IHttpActionResult GetAndPostStoryComments([FromBody] BlogAndStoryComment comment)
    {
       //some business logic
    }
}

下面是webapi注册方法:

public static void Register(HttpConfiguration config)
{
    // Web API routes
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
        "DefaultApi",
        "api/{controller}/{action}/{id}",
        new { id = RouteParameter.Optional }
    );
    var f = new FormUrlEncodedMediaTypeFormatter();
    f.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
    f.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/x-www-form-urlencoded"));
    config.Formatters.Add(f);
    var enableCorsAttribute = new EnableCorsAttribute("*",
                                       "Origin, Content-Type, Accept",
                                       "GET, PUT, POST, DELETE, OPTIONS");
    config.EnableCors(enableCorsAttribute);
}

我的代码哪里出错了,我该如何解决这个问题?

【问题讨论】:

  • [VersionedRoute("", 1)] 这就是问题所在。要对其进行测试,请从上述操作中删除该属性,并且应该解决冲突。

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


【解决方案1】:

示例中所有版本化路由的模板都是相同的。这就是路线冲突的原因。更新版本化路由模板以使其唯一或完全删除它们以解决路由冲突。

public class SmartlingController : BaseApiController {
    //POST api/smartling/ProcessSmartlingTranslation
    [Route("api/smartling/ProcessSmartlingTranslation")]
    [VersionedRoute("api/smartling/ProcessSmartlingTranslation", 1)]
    [ResponseType(typeof(HttpResponseMessage))]
    [HttpPost]
    public IHttpActionResult ProcessSmartlingTranslation(HttpRequestMessage request)  {
       //some business logic
    }
}

public class CommentsController : BaseApiController {
    //POST api/comments/GetAndPostBlogComments
    [Route("api/comments/GetAndPostBlogComments")]
    [VersionedRoute("api/comments/GetAndPostBlogComments", 1)]
    [ResponseType(typeof(HttpResponseMessage))]
    [HttpPost]
    public IHttpActionResult GetAndPostBlogComments([FromBody] BlogAndStoryComment comment) {
       //some business logic
    }

    //POST api/comments/GetAndPostStoryComments
    [Route("api/comments/GetAndPostStoryComments")]
    [VersionedRoute("api/comments/GetAndPostStoryComments", 1)]
    [ResponseType(typeof(HttpResponseMessage))]
    [HttpPost]
    public IHttpActionResult GetAndPostStoryComments([FromBody] BlogAndStoryComment comment) {
       //some business logic
    }
}

【讨论】:

  • 是的,你是对的 Nkosi,我刚刚删除了 versionedroute,问题就解决了。我正在投票并将此答案标记为解决方案。
猜你喜欢
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 2018-08-07
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2023-01-27
  • 2019-11-25
相关资源
最近更新 更多