locked
Route Mapping RRS feed

  • Question

  • User-1995896158 posted

    I try to do a route map for 2 controller: home and account. I set the default one as account, but every time that i call the home one the app go on account one.

    this is the routeConfig.cs file:

    routes.MapRoute(
    name: "Account",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Account", action = "Accedi", id = UrlParameter.Optional }
    );
    routes.MapRoute(
    name: "Home",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

    this is where i try to call the cotroller:

    @Html.ActionLink("All-Projects", "Index", "Home", new { @class = "navbar-brand" })

    Saturday, September 14, 2019 6:52 PM

Answers

  • User-17257777 posted

    Hi jacopoFabbri,

    Your overloaded ActionLink is incorrect, two ways can solve this issue.

    1.  

    @Html.ActionLink("All-Projects", "Index", new { controller = "Home" }, new { @class = "navbar-brand" })

    2. 

    @Html.ActionLink("All-Projects", "Index", "Home", null, new { @class = "navbar-brand" })

    Test Result:

    You can also refer to this for more details of ActionLink.

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 16, 2019 2:40 AM

All replies

  • User-17257777 posted

    Hi jacopoFabbri,

    Your overloaded ActionLink is incorrect, two ways can solve this issue.

    1.  

    @Html.ActionLink("All-Projects", "Index", new { controller = "Home" }, new { @class = "navbar-brand" })

    2. 

    @Html.ActionLink("All-Projects", "Index", "Home", null, new { @class = "navbar-brand" })

    Test Result:

    You can also refer to this for more details of ActionLink.

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 16, 2019 2:40 AM
  • User409696431 posted

    Is there a reason why you are using the same url in both routes?  A route that matches everything and is normally only used for the default that sends to Home?  That means the first match - Account - will always be found.

    If you want the url for Account to be "Account/{id}",  set the url to that.  Explicitly spell out Account.

    Tuesday, September 17, 2019 5:24 AM
  • User-474980206 posted

    Because your routes have the same pattern match, the first one is always selected, as there is no case where if not selected by the first, it would Be selected by the second.
    Wednesday, September 18, 2019 12:47 AM
  • User-1339639702 posted

    Hi, 

    As i can see here two route defined but there is no difference. Also i can see both have the route name "defaults".

    To make it work first change one of the route and pass extra parameters which should match according to your request. Change that route name to some other name.

    If you would like to call the modified route can use attribute routing inside controller level or particular action level.

    Thanks

    Tuesday, September 24, 2019 2:06 PM