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

后端 未结 2 1340
盖世英雄少女心
盖世英雄少女心 2021-02-10 22:02

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. Th

相关标签:
2条回答
  • 2021-02-10 22:41
    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.)

    0 讨论(0)
  • 2021-02-10 22:44

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

    0 讨论(0)
提交回复
热议问题