User987
User987

Reputation: 3825

.NET MVC Route not working propely

I have defined my routes in .NET app like this:

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

    routes.MapRoute(
        name: "Register",
        url: "Register",
        defaults: new { controller = "Home", action = "Register" }
    );

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

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

So that I can have routes like following:

mysite.com/Login
mysite.com/Register
mysite.com/ResetPwd

Now the problem arises with other controller when I try to redirect the user to dashboard when hes logged in successfully from mysite.com/Login like this:

return RedirectToAction("Index", "Dashboard");

However I'm getting an error:

The resource cannot be found.

I presume it want's me to define a route now for dashboard/index... but isn't that nuts since then I'd have to add every route manually ???

What am I doing wrong here?

Upvotes: 0

Views: 373

Answers (1)

Ali Baig
Ali Baig

Reputation: 3867

Change your resetpassword route to

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

Currently your provided url /Dashboard/Indexis looked up by this route with Dashboard being an Action on Default Home controller which doesn't exists in my opinion.

Upvotes: 1

Related Questions