我正在使用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);
}
我的代码在哪里错了,我该如何解决这个问题?
答案 0 :(得分:2)
示例中所有版本化路由的模板都是相同的。这就是路线冲突的原因。更新版本化路由模板以使其唯一或完全删除它们以解决路由冲突。
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
}
}