简体   繁体   English

在ASP.net 5 MVC 6中,如何在不同的命名空间中使用相同的控制器名称

[英]In ASP.net 5 MVC 6 , How to use same controller name in different namespaces

I defined two controller with same controller name in different namespaces. 我在不同的名称空间中定义了两个具有相同控制器名称的控制器。 And got an exception. 并且有一个例外。 How to uer parameter "dataTokens" to define namespace of controller like mvc-4? 如何使用参数“ dataTokens”来定义像mvc-4这样的控制器的命名空间?

Exception below: 以下例外:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

Alice.Controllers.TestController.Index
Alice.Controllers.Api.TestController.Index
Microsoft.AspNet.Mvc.Infrastructure.DefaultActionSelector.SelectAsync(RouteContext context)

Controllers/Api/TestController.cs : 控制器/Api/TestController.cs:

namespace Alice.Controllers.Api
{
    //[Route("api/[controller]")]
    public class TestController : Controller
    {
        //[Route("[action]")]
        public string Index()
        {
            return "this is controller at Alice.Controllers.Api"; ;
        }
    }
}

Controllers/TestController.cs : Controllers / TestController.cs:

namespace Alice.Controllers
{
    //[Route("[controller]")]
    public class TestController : Controller
    {
        //[Route("[action]")]
        public string Index()
        {
            return "this is controller at Alice.Controllers";
        }
    }
}

Startup.cs Startup.cs

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action}",
                defaults: null,
                constraints: null,
                dataTokens: new { Namespaces = new[] { "Alice.Controllers" } });

            routes.MapRoute(
                name: "api",
                template: "api/{controller}/{action}",
                defaults: null,
                constraints: null,
                dataTokens: new { Namespaces = new[] { "Alice.Controllers.Api" } });
        });

If more details need please ask. 如果需要更多详细信息,请询问。

Unfortunately by default you cannot have duplicate controller names in ASPNET MVC Areas (or between an Area and the root of the application). 不幸的是,默认情况下,您在ASPNET MVC区域(或区域和应用程序的根之间)不能有重复的控制器名称。 Fortunately, the fix for this is pretty simple, and the exception describes the step you need to take. 幸运的是,此问题的修复非常简单,并且异常描述了您需要执行的步骤。 Once you've added an Area, you will have two different places (by default) where routes are defined: one in your root application and one in your area registration. 添加区域后,将在两个不同的地方(默认情况下)定义路由:一个在根应用程序中,一个在区域注册中。 You will want to adjust both of them to specify a namespace parameter. 您将需要调整它们两者以指定名称空间参数。 more details check here 更多细节请点击这里

Namespaces are not an MVC feature. 命名空间不是MVC功能。 Controllers are simply classes. 控制器只是类。 If you need two controllers that are basically the same, then derive them from a common class and put them in whatever namespace you want. 如果您需要两个基本相同的控制器,则可以从一个通用类派生它们,并将它们放在所需的任何命名空间中。

startUp.cs startUp.cs

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "areaRoute",
                template: "{area:exists:regex(^(?!Main$).)}/{controller=Home}/{action=Index}/{id?}");

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}",
                defaults: new { area = "Main"});
        });

Areas:Main// area default : localhost/home/index 区域:Main //默认区域:localhost / home / index

namespace Exzen.Areas.Main.Controllers
{
    [Area("Main")]    
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Areas:Test// area plus: localhost/test/home/index 区域:Test //区域加:localhost / test / home / index

namespace Exzen.Areas.Test.Controllers
{
    [Area("Test")]    
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

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

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