sui.zhiyuan
sui.zhiyuan

Reputation: 23

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?

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.

Upvotes: 2

Views: 4629

Answers (3)

Nguyễn Văn Đức
Nguyễn Văn Đức

Reputation: 11

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();
        }
    }
}

Upvotes: 1

Anik Saha
Anik Saha

Reputation: 4494

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

Upvotes: 2

Leonardo Herrera
Leonardo Herrera

Reputation: 8406

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.

Upvotes: 1

Related Questions