How can I combine a MVC4 site and a WebAPI site under the same IIS website?

无人久伴 提交于 2020-01-31 05:49:04

问题


I have a project (named Product.Api) that has some controllers used for our WebAPI site. The controllers are in a namespace Product.Api.Controllers. The project also has some other helper stuff as well. This is compiled into a library.

I also have another regular ASP.NET MVC4 website named Product.Web. This has Controllers (under the namespace Product.Web.Controllers), Models and Views (in its corresponding folders). It also has a Global.asax.cs which has the routing information.

When the site is created in IIS (.NET 4.5, IIS7, integrated mode, etc), I copy the Product.Api dll into the bin folder. The Product.Web is the main site.

Now I want the url http://www.product.com/api/{Controller}/{id} to map to the WebAPI stuff and http://www.product.com/{Controller}/{Action}/{id} to map to the regular MVC.

This is what I have right now as my routes:

routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}", 
                defaults: new {id = RouteParameter.Optional}
            ).Constraints["Namespaces"] = new string[] { "Product.Api.Controllers" };

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ,
                namespaces: new[] { "Product.Web.Controllers" }
            );

Unfortunately when I go to an api url (in this case the example was http://www.product.com/api/Tables/ (or even when I define an id), I get a HTTP 404 error.

http://www.product.com/Home/Index works fine.

Oddly enough I tried the routing debugger tool and I still had issues. In fact I could never see the output of the debugger. http://www.product.com/Home/Index and http://www.product.com/ both show the view but everything api/* still shows 404. I have yet to see any output from the debugger.

So is there anyway to get the desired effect?

Edit: I noticed that my global.asax.cs file was in the Product.Web namespace. I changed it to be in the Product namespace and still no go.


回答1:


routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}", 
                defaults: new {id = RouteParameter.Optional}
            ).Constraints["Namespaces"] = new string[] { "Product.Api.Controllers" };

this will not work with MapHttpRoute.

If you want to use ApiController routes from the other bin, just remove the constraints, the runtime will automatically pick up your ApiControllers from the bin. MapHttpRoute will not interfere with your MVC controllers and vice versa, so the constraints are not needed anyway.

Alternatively use this to contraint your HttpRoute namespace

routes.DataTokens["Namespaces"] = new string[] {"MyNamespace"};

Or use this solution (but that's really only needed if you keep stuff outside of bin.)




回答2:


Before the MapHttpRoute Factory call add System.Web.Mvc.ControllerBuilder.Current.DefaultNamespaces.Add("Namespace.Full.Controllers");



来源:https://stackoverflow.com/questions/11640985/how-can-i-combine-a-mvc4-site-and-a-webapi-site-under-the-same-iis-website

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