MVC routing not working as needed

28 Views Asked by At

I'm trying to set up a custom route in MVC but having a few problems.

This is the config file so far: -

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


        routes.MapRoute(
            name: "inbox",
            url: "inbox",
            defaults: new { controller = "Mail", action = "Inbox", id = UrlParameter.Optional }
        );


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

    }
}

This is the link in my view: -

@Html.ActionLink("Inbox", "inbox")

The trouble is the output html is being produces as http://localhost/Home/inbox

I just want it to be http://localhost/inbox

Thanks for any help

1

There are 1 best solutions below

0
On BEST ANSWER

You are using the wrong overload of Html.ActionLink. With the version you are using, it uses the current context to determine the controller, whereas you want to use the Mail controller. So to fix this, change it to explicitly specify the controller like this:

@Html.ActionLink("Inbox", "Inbox", "Mail")