Unable to hit duplicate controller route after ins

2019-09-05 15:29发布

问题:

Recently i installed Fluent

I have two projects - one MVC and other Web Api project. Both have same controller named as Deals controller. After fluent installation, i got problem that web api request is not able to locate any DealsController.

In MVC's Gloabal asax- Application_Start method:

protected void Application_Start(object sender, EventArgs e)
{
            System.Web.Http.GlobalConfiguration.Configure(CompanyName.Service.WebApiConfig.Register);
            CompanyName.RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);

}

Both these statements registers the routes for mvc and web api respectively.

I have DealsController, which is located with the same name in both the projects (MVC and Web Api).

This is MVC's DealsController:

public class DealsController : System.Web.Mvc.Controller
{
    public DealsController()
    {

    }
}

This is Web Apis Deals controller:

public class DealsController : System.Web.Http.ApiController
{
    public DealsController()
        {

        }

    [Route("api/advantage/getDeals/")]
    public IHttpActionResult GetDeals([FromUri] CompanyName.Entity.Deals.Filters filter)
    {
    }
}

Post fluentvalidation.webapi installation from nuget, when i am hitting this web api's method: http://localhost/api/advantage/getDeals/?cityId=2&sc=0&so=1&pn=1

I am getting following error:

"Message": "No HTTP resource was found that matches the request URI 'http://localhost/api/advantage/getDeals/?cityId=2&sc=0&so=1&pn=1'.",

"MessageDetail": "No type was found that matches the controller named 'advantage'."

I dont know what exactly is happening post installation of fluent validation web api package. I just installed fluentvalidation.webapi package which gave me some cors related error, so i further installed Microsoft.AspNet.WebApi.Cors.

If i am renaming My web api controller (dealscontroller) to some other name, it is working fine.

But i don't understand what is exactly happening here and what other risk this update of fluentvalidation.webapi can have in my solution and other apis.

回答1:

I think when you are installing FluentValidation.WebAPI it is upgrading the Microsoft.AspNet.WebApi or other Web API libraries (although that shouldn't happen). If not this Microsoft.AspNet.WebApi.Cors will definitely change the version (if you are not on the latest version).

So you need to make sure the version of Web API libraries is same in both MVC project and class library.