0

I'm new to MVC Framework; I'm trying to learn.

I'm trying to Redirect from Login to Home Page

[HttpPost]
public ActionResult Login(Register model)
{
    StudentDBHandle db = new StudentDBHandle();
    DataTable dst = db.Login(model);
    if (dst.Rows.Count==1)
        return RedirectToAction("Index","Student");
    else
    {
        ViewBag.Message = "Login Failed";
        return View();
    }
}    

it says "No route in the route table matches the supplied values."

So I Added Route in routeconfig file

public static void RegisterRoutes(RouteCollection routes
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        name: "Login",
        url: "{controller}",
        defaults: new { controller = "Register", action = "Login", id = UrlParameter.Optional }
    );

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

When the login succeeds, it redirects to the same Login page

I have changed the order, but it's not working.

Kahbazi
  • 14,331
  • 3
  • 45
  • 76
Prakash N
  • 54
  • 8
  • 2
    Your first route makes no sense and needs to be deleted –  Aug 18 '17 at 00:51
  • 2
    And if its redirecting back to the login page, then I assume you have `[Authorize]` on your other method(s) –  Aug 18 '17 at 00:54
  • When the app builds, landing page should be login page. Thats why i added first route.. – Prakash N Aug 18 '17 at 02:46
  • 1
    Then delete the first route and change the 2nd to `defaults: new { controller = "Register", action = "Login", id = UrlParameter.Optional }`. But that is not the correct usage anyway. The default should be you 'home' page, and when your authorization is set up, then when a user navigates to any page and is not authorized, they will be redirected automatically to the Login page. but clearly you have not set up authorization and your `Login` method makes no sense - your not doing any authorization! –  Aug 18 '17 at 02:50
  • 1
    Yes, but the code in your Login method does not do any authorization! I suggest you go to the MVC site and work through the section on _Security, Authentication and Authorization_ –  Aug 18 '17 at 02:52
  • Thank you for your information. Let me initiate.. – Prakash N Aug 18 '17 at 02:55

0 Answers0