1

I want to change the route display name in URL, like below:

Access UserController display: https://localhost:7214/admin/login

Access CustomerController display: https://localhost:7214/customers/index

UserController.cs

[Route("admin")]
public class UserController : Controller
{
    [AllowAnonymous]
    public IActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;

        return View();
    }
}

CustomerController.cs

[Route("customers")]
public class CustomerController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Program.cs

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Admin}/{action=Login}/{id?}");

app.Run();

But when I run the program, hit the following error:

enter image description here

I don't want to use Areas, any other options other than Areas?

Updated:

I modified code below in Program.cs

app.MapControllerRoute(
    name: "customers",
    pattern: "customers/{action}/{id?}",
    defaults: new { controller = "Customer", action = "Index" }); 

Access index page is find, but access route method, hit error 404.

public class CustomerController : Controller
{
    public IActionResult Index()  <-- working fine
    {
        return View();
    }

    [Route("test/export")]
    public IActionResult Test()  <-- error 404
    {
        return View();
    }
}

enter image description here

2 Answers 2

3

Routing from Contoller using Route attribute,

UserController:

[Route("admin/[action]/{id?}")]
public class UserController : Controller
{
    [AllowAnonymous]
    public IActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;

        return View();
    }
}

CustomerController:

[Route("customers/[action]/{id?}")]
public class CustomerController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Or you can add route in Startup.cs (Program.cs file for 6.0 and up).

Multiple conventional routes:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.MapControllerRoute(
                name: "demo",
                pattern: "admin/{action}/{id?}",
                defaults: new { controller = "User", action = "Login" });

app.MapControllerRoute(
                name: "demo2",
                pattern: "customers/{action}/{id?}",
                defaults: new { controller = "Customer", action = "Index" });

Reference: Asp.net core Docs https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-6.0

Sign up to request clarification or add additional context in comments.

10 Comments

Hi, please refer my updated code, i have error 404 again if follow second method.
[Route("test")] --> remove this
Thanks, but I found another issue on second approach, for the customer, I tried both URL /customers/index or /customer/index , realized that without s also able to access the page
For first approach, if I add [Route("homes/{action}/{id?}")] on start up page controller will hit 404 too,
" for the customer, I tried both URL /customers/index or /customer/index , realized that without s also able to access the page...." /customer/index works because of app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
|
0

Since you have applied the attribute routing, the actions method should also be applied with attribute routing too.

Solution 1

UserController.cs

[Route("admin")]
public class UserController : Controller
{
    [AllowAnonymous]
    [HttpGet("login")]
    public IActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;

        return View();
    }
}

CustomerController.cs

[Route("customers")]
public class CustomerController : Controller
{
    [HttpGet("index")]
    public IActionResult Index()
    {
        return View();
    }
}

Demo

enter image description here


Solution 2

Refer to the demo for defining the route for Controller (level) with Attribute Routing,

[Route("api/{controller}/{action}/{id?:int}")]
public abstract class BaseApiController : ControllerBase, IApiController
{
    // ...
}

UserController.cs

[Route("admin/{action}/{id?}")]
public class UserController : Controller
{
    [AllowAnonymous]
    public IActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;

        return View();
    }
}

CustomerController.cs

[Route("customers/{action}/{id?}")]
public class CustomerController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Demo

enter image description here

5 Comments

But it's not work on start up page, your default page is Home, if set the route on Home will hit error 404.
Please refer my updated code, even I tried use [Route("customers/{action}/{id?}")] , still have error during access test method
I see, how about the issue [Route("homes/{action}/{id?}")] ? I try set this on default start up page, hit the 404, should I do any changes on Program.cs?
Do you mind sharing your repository for reproducing the error you mentioned?
I uploaded the code, github.com/howardhee/RoutingError.git Just added [Route("homes/{action}/{id?}")] on Home controller then hit the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.