Attribute routes on multiple controllers match the requested url

I am using a web api project where I used two controllers:

The first controller looks like this:

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 the webapi registration 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 can I fix this problem?

+3


source to share


1 answer


The template for all versions of routes in the example is the same. This is why there are conflicting routes. Update the versioned route templates to make them unique or remove them entirely to resolve 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
    }
}

      

+2


source







All Articles