简体   繁体   English

如果作为子应用程序部署在IIS中,则ASP.NET Web API中的路由问题

[英]Routing problems in ASP.NET Web API if deployed in IIS as child application

I have a solution in VS2013 with multiple ASP.NET Web applications. 我在VS2013中具有多个ASP.NET Web应用程序的解决方案。 I have added an ASP.NET WebAPI project in the same solution. 我在同一解决方案中添加了一个ASP.NET WebAPI项目。

After I deployed the WebAPI as a child application in IIS I got the following error: 在将WebAPI部署为IIS中的子应用程序后,出现以下错误:

Multiple types were found that match the controller named 'Account'. 找到了多个与名为“帐户”的控制器匹配的类型。 This can happen if the route that services this request ('Account/Register') does not specify namespaces to search for a controller that matches the request. 如果为该请求提供服务的路由(“帐户/注册”)未指定名称空间来搜索与该请求匹配的控制器,则会发生这种情况。 If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. 如果是这种情况,请通过调用带有“名称空间”参数的“ MapRoute”方法的重载来注册此路由。

The request for 'Account' has found the following matching controllers: “帐户”请求已找到以下匹配的控制器:
Proj1.Controllers.AccountController Proj1.Controllers.AccountController
Proj2.Controllers.AccountController Proj2.Controllers.AccountController

I have tried adding a default namespace to the RouteConfig.cs like so: 我尝试将默认名称空间添加到RouteConfig.cs如下所示:

routes.MapRoute(

                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "Proj1.WebAPI.Controllers" }

            );

but I still get the same error. 但我仍然遇到相同的错误。

Any ideas? 有任何想法吗?

I just needed to add this to my WebApiConfig.cs 我只需要将其添加到我的WebApiConfig.cs

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "webapi/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        **config.Routes.MapHttpRoute(
            name: "Admin",
            routeTemplate: "{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }**
        );

    }

Works perfectly now! 现在工作完美!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM