Reserved ASP.NET MVC Controller Names?

点点圈 提交于 2019-12-23 19:40:19

问题


So I just spent what felt like an hour debugging why I was receiving a client-side 404 and a server-side System.Web.Http.HttpResponseException on System.Web.Http.dll!System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(System.Net.Http.HttpRequestMessage request) + 0x33f byte; no matter what I tried I could not get my ApiController derivative to receive requests.

In the end, after trying to convert it to a regular Controller to no effect, it occurred to me to change the name from APIController to APIXController only to have everything work.

  1. Where is it stated that API is a reserved controller name?
  2. Where is the full list of reserved controller names?
  3. Is it possible to bypass this restriction on using APIController as my class name, and if so, how do I go about doing so?

One of the first things I checked was my route configuration, which is the bog standard

routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

回答1:


APIController is not a reserved name. But, if you're using a standard MVC4 template, then by default it includes an instance of a WebAPI stack, and this registers a route that starts with /api

You were most likely running into that.

Look under AppStart and in the WebApiConfig.cs file.

By the way, the key debugging symptom is anything in System.Web.Http, that is used by WebApi, and would not show up in an MVC app.



来源:https://stackoverflow.com/questions/15262243/reserved-asp-net-mvc-controller-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!