问题
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.
- Where is it stated that
API
is a reserved controller name? - Where is the full list of reserved controller names?
- 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