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

允我心安 提交于 2019-12-10 11:07:02

问题


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?

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 :

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 :

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

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.


回答1:


Unfortunately by default you cannot have duplicate controller names in ASPNET MVC Areas (or between an Area and the root of the application). 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




回答2:


Namespaces are not an MVC feature. 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.




回答3:


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

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

Areas:Test// area plus: localhost/test/home/index

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


来源:https://stackoverflow.com/questions/34472948/in-asp-net-5-mvc-6-how-to-use-same-controller-name-in-different-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!