Defining new map controller route in ASP NET

Jack Herer 110 Reputation points
2023-04-23T19:02:29.7666667+00:00
app.MapControllerRoute(
    name: "Route Name",
    pattern: "Pattern in page url",
    defaults: new
    {
        Controller = "Name of controller for this route",
        Action = "Action in controller"
    }
    );
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-04-24T03:13:21.05+00:00

    Hi @Jack Herer

    Defining new map controller route in ASP NET

    app.MapControllerRoute(
        name: "Route Name",
        pattern: "Pattern in page url",
        defaults: new
        {
            Controller = "Name of controller for this route",
            Action = "Action in controller"
        }
        );
    

    What's your question, can you explain more detail about it? You can use the above code to add a conventional routing in Asp.net core MVC/API application, like this:

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

    it same with the following code:

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

    The preceding is an example of a conventional route. It's called conventional routing because it establishes a convention for URL paths:

    • The first path segment, {controller=Home}, maps to the controller name.
    • The second segment, {action=Index}, maps to the action name.
    • The third segment, {id?} is used for an optional id. The ? in {id?} makes it optional. id is used to map to a model entity. More detail information about routing, see Routing to controller actions in ASP.NET Core

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.