Is “api/” reserved in ASP.NET MVC routes?

2019-08-12 21:56发布

I don't want to use ASP.NET MVC WebApi, but I do want to use the URL /api/controller/action so that it is handled by my own controller

I'm doing this:

        routes.MapRoute(
            name: "APIRoute",
            url: "api/{controller}/{action}/",
            namespaces: new[] { "MyProject.Controllers.APIControllers" }

But it seems that is not possible to use api/ in MapRoute(...)

If I navigate to http://localhost/api/Blah/List it doesn't work

If I change the route to this:

        routes.MapRoute(
            name: "APIRoute",
            url: "apix/{controller}/{action}/",
            namespaces: new[] { "MyProject.Controllers.APIControllers" }

        (Notice the 'x' in 'apix/')

And then I navigate to http://localhost/apix/Blah/List it works as expected

What can I do? is it possible to override api/ ?

1条回答
Anthone
2楼-- · 2019-08-12 22:50

Sounds like you may have created a new project which includes Web API which is why you're having a hard time.

Search your project for WebApiConfig or DefaultApi - if you have any results then Web API has been added to your project and you'll want to amend the default routeTemplate which is by default api/{controller}/{id}.

查看更多
登录 后发表回答