How do I register a controller that has been created in an AREA

守給你的承諾、 提交于 2019-12-28 07:05:48

问题


I am using MVC4's WEB API to expose a controller.

Initially I created created a MVC4 WEBAPI project, set the project not to open any web page, wait for an external app to call the URL (WEB API). The project in VS2010 is set to run from IIS7 Express, not VS2010's Dev Server. This works OK, the browser prompts me to down load a file. Which is OK for me, as the browser does not know what to do with the returned data (RAW TEXT).

Next, I created an AREA in the MVC4 project area, then added a controller (WEB API type).

Then I once again ran the project and in a browser entered the the URL (WEB API). And it fails.

Ed.


回答1:


The DefaultHttpControllerFactory doesn't work with Areas by default. To get this functionality you have to write your custom HttpControllerFactory.

You can find how to do this at the post How to create HttpControllerFactory to support Areas




回答2:


While it is possible to place WebApi Controllers in different Areas, ASP.NET MVC 4 RC will still treat these ApiControllers as if they all reside in the same namespace. This is a limitation of the DefaultHttpControllerSelector, which is responsible for selecting the appropriate ApiController.

Fortunately, you can inject your own implementation of this class. In fact, I've already encountered this very issue and written an "area aware" HttpControllerSelector. You can find a blog post of mine about this issue and its solution here:

http://blogs.infosupport.com/asp-net-mvc-4-rc-getting-webapi-and-areas-to-play-nicely/



来源:https://stackoverflow.com/questions/9485934/how-do-i-register-a-controller-that-has-been-created-in-an-area

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