Reputation: 85
I am using web api project where I used two controllers:
The first controller is like below:
public class SmartlingController : BaseApiController
{
[Route("api/smartling/ProcessSmartlingTranslation")]
[VersionedRoute("", 1)]
[ResponseType(typeof(HttpResponseMessage))]
[HttpPost]
public IHttpActionResult ProcessSmartlingTranslation(HttpRequestMessage request)
{
//some business logic
}
}
Second 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
}
}
Below is webapi register method:
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);
}
Where is my code wrong here and how could I fix this issue?
Upvotes: 3
Views: 165
Reputation: 247088
The template for all the versioned routes in the example are the same. That is the reason for the conflicting routes. Update the versioned route templates to make them unique or remove them altogether to resolve the route conflicts.
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
}
}
Upvotes: 2