gpt4 book ai didi

c# - 多个 Controller 上的属性路由匹配请求的 url

转载 作者:行者123 更新时间:2023-11-30 16:44:51 25 4
gpt4 key购买 nike

我正在使用我使用两个 Controller 的 web api 项目:

第一个 Controller 如下:

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

第二个 Controller :

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);
}

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

最佳答案

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

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
}
}

关于c# - 多个 Controller 上的属性路由匹配请求的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com