如果作为子应用程序部署在 IIS 中,ASP.NET Web API 中的路由问题
Routing problems in ASP.NET Web API if deployed in IIS as child application
我在 VS2013 中有一个包含多个 ASP.NET Web 应用程序的解决方案。我在同一解决方案中添加了一个 ASP.NET WebAPI 项目。
在 IIS 中将 WebAPI 作为子应用程序部署后,出现以下错误:
发现多个类型与名为 'Account' 的控制器相匹配。如果为该请求提供服务的路由 ('Account/Register') 未指定名称空间来搜索与请求匹配的控制器,则可能会发生这种情况。如果是这种情况,请通过调用带有 'namespaces' 参数的 'MapRoute' 方法的重载来注册此路由。
'Account' 的请求具有找到以下匹配的控制器:
Proj1.Controllers.AccountController
Proj2.Controllers.AccountController
我试过向 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" }
);
但我仍然得到同样的错误。
有什么想法吗?
我只需要将它添加到我的 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 }**
);
}
现在完美运行!
我在 VS2013 中有一个包含多个 ASP.NET Web 应用程序的解决方案。我在同一解决方案中添加了一个 ASP.NET WebAPI 项目。
在 IIS 中将 WebAPI 作为子应用程序部署后,出现以下错误:
发现多个类型与名为 'Account' 的控制器相匹配。如果为该请求提供服务的路由 ('Account/Register') 未指定名称空间来搜索与请求匹配的控制器,则可能会发生这种情况。如果是这种情况,请通过调用带有 'namespaces' 参数的 'MapRoute' 方法的重载来注册此路由。
'Account' 的请求具有找到以下匹配的控制器:
Proj1.Controllers.AccountController
Proj2.Controllers.AccountController
我试过向 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" }
);
但我仍然得到同样的错误。
有什么想法吗?
我只需要将它添加到我的 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 }**
);
}
现在完美运行!