简体   繁体   English

具有相同控制器名称(不同名称空间)的ASP.net MVC单独解决方案

[英]ASP.net MVC seperate solutions with same controller name (different namespace)

I have some problems when I have same controller name in separate projects. 当我在单独的项目中使用相同的控制器名称时,我会遇到一些问题。 My main solution is Web forms, and I have two MVC separate projects(separate folder) , the problem If I have a controller in first project with name HomePage and same controller name in solution 2 I have an error: Multiple types were found that match the controller named 'HomePage'. 我的主要解决方案是Web表单,并且我有两个MVC单独的项目(单独的文件夹),问题是,如果我在第一个项目中有一个名称为HomePage的控制器,并且在解决方案2中具有相同的控制器名称,则我遇到一个错误:找到了多个匹配的类型名为“ HomePage”的控制器。 This can happen if the route that services this request ('{*pathInfo}') does not specify namespaces to search for a controller that matches the request. 如果为该请求提供服务的路由('{* pathInfo}')未指定名称空间来搜索与该请求匹配的控制器,则会发生这种情况。 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 'HomePage' has found the following matching controllers: Project1.Controllers.HomePageController Project2.Controllers.HomePageController 对“ HomePage”的请求已找到以下匹配的控制器:Project1.Controllers.HomePageController Project2.Controllers.HomePageController

The global.asa is in the web forms solution which I added two routes map but I still have same error. global.asa在Web表单解决方案中,我添加了两个路线图,但仍然有相同的错误。 Any solution to fix this issue? 有解决此问题的解决方案吗? Can I can in the view the action with namespace @Html.Action("index", "HomePage") Thank you 我可以在视图中使用名称空间@ Html.Action(“ index”,“ HomePage”)进行操作谢谢

If you need to set the namespaces value when registering the route, you should do so like this: 如果在注册路由时需要设置名称空间值,则应这样进行:

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

By specifying the namespace, it removes the ambiguity. 通过指定名称空间,它消除了歧义。

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

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