Max A Max A - 1 year ago 65
ASP.NET (C#) Question

Open a link without a controller selected in url


I have a ASP.NET application where I have different controllers. I changed in the RouteConfig the route to:


new { controller = "Home", action = "Index", id = UrlParameter.Optional });

name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Error", action = "NotFound", id = UrlParameter.Optional }

All pages in my the HomeController are working fine. One way I go to them is with a ActionLink:

@Html.ActionLink("Index Page", "Index", "Home")

And that works fine, even as other ways with the context as: "example", "Home".

It goes wrong when I select an other controller, let's say:

@Html.ActionLink("GoToErrorPage", "NotFound", "Error") // error page

The result of this link is that it just connects to the home page. It does not go the the correct page.

I want to connect to all pages (in other controller's too) with a URL like:


How the original url looked like (before the change in the RouteConfig)



  1. How do I need to configure the RouteConfig?

  2. How do I set it that I don't need to type the controller before the
    action in the url?

  3. How do I set the ActionLink correct?

Answer Source

Both your routes are effectively the same in that they both match any url with 2 segments. If you want to be able to navigate to a method without specifying the controller name, then you will need to create specific routes for each of those methods. For example if you want to navigate to the Request(int? id) method of HomeController, with ../Request or ../Request/1 then you need

    new { controller = "Home", action = "Request", id = UrlParameter.Optional }

and that route must be placed before the default route

Then in the view @Html.ActionLink("Request Page", "Request", "Home") will generate a url of ../Request and go the the Request() method of HomeController

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download