Reputation: 705
I have two controller based on user type in my ASP.NET Core project. One is AdminController
for admin and one is UserController
for users. And there is also HomeController
for signin and contact page. I am using following map route configuration for both admin and user controllers.
config.MapRoute(
name: "UserRoute",
template: "{controller}/{username}/{action}",
defaults: new { controller = "User|Admin", action = "Dashboard" }
);
By using above route config I am getting following types of Urls
/User/user1
/Admin/user2
I don't want the Admin
and User
part in URL instead I want
/user1
/user2
How to remove User
and Admin
part from the URL? If I remove controller from {controller}/{username}/{action}
and specify only controller in defaults then it only works for one controller.
Upvotes: 2
Views: 1242
Reputation: 4456
You can't have the same URL template with 2 default controllers, because mvc will not understand which controller to use.
You can either have 2 routes and each one with /Admin and /User, just like this:
config.MapRoute(
name: "UserRoute",
template: "User/{username}/{action}",
defaults: new { controller = "User", action = "Dashboard" }
);
config.MapRoute(
name: "AdminRoute",
template: "Admin/{username}/{action}",
defaults: new { controller = "Admin", action = "Dashboard" }
);
And from the Home controller, you can check the user role and redirect him to the correct route.
Another approach, would be only one route as you need
config.MapRoute(
name: "UserRoute",
template: "{username}/{action}",
defaults: new { controller = "User", action = "Dashboard" }
);
But in this version, you will have only one controller and you can enable or disable actions according to the user role
Upvotes: 1