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

↘锁芯ラ 提交于 2019-12-03 08:34:29
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.)

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

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