0

I have a controller name Dashboard and inside that controller i have an action AdminDashboard . Now by default url of this action becomes /Dashboard/AdminDashboard . I want to map this action to this url /SupervisorDashboard

This is what i am doing but its saying not found

routes.MapRoute(
            name: "SupervisorDashboard",
            url: "SupervisorDashboard",
            defaults: new { controller = "Dashboard", action = "AdminDashboard" }
        );

and also how can i redirect to this page using Url.Action

Global.asax

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "SupervisorDashboard",
            url: "SupervisorDashboard",
            defaults: new { controller = "Dashboard", action = "AdminDashboard" }
        );

    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}
1
  • Is public static void RegisterRoutes(RouteCollection routes) called from anywhere? From what I see, real routing is defined in RouteConfig.RegisterRoutes(RouteTable.Routes); inside Application_Start, check that method. Commented Mar 27, 2015 at 7:56

1 Answer 1

2

Have you placed this new route definition before default route? Routes are evaluated in the same order in which they were registered. If you put default route before any of custom routes, it will be used (and since you probably don't have any SupervisorDashboardController in code, 404 will be returned).

Url.Action should work correctly, if routes are defined in correct order.

So, for this case, RouteConfig should look like this:

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

        // this one match /SupervisorDashboard only
        routes.MapRoute(
            name: "SupervisorDashboard",
            url: "SupervisorDashboard",
            defaults: new { controller = "Dashboard", action = "AdminDashboard" }
        );

        // should be last, after any custom route definition
        routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );
    }
} 
Sign up to request clarification or add additional context in comments.

6 Comments

what if i didn't have any default route?
and also is this correct Url.Action("SupervisorDashboard") ?
No, just use Url.Action("AdminDashboard", "Dashboard"), routing will take care of translation to correct url.
ok but routing still not working and i also don't have any home controller and default route in my global.asax,, any idea ?
Hmm, could you edit answer and post some code related to routing? Ideally whole routing definition. Default route is usually route, which can catch all requests. It is most probably route with argument url: "{controller}/{action}/{id}". Such route should be placed as last, because it can match any request done to application.
|

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.