1

I am new to MVC and I m building a website using ASP.Net MVC5.I m a little confused with the routing concept. The following is my route.config file that I have. so Whenever I go to my home page or login page and enter my credentials and click the submit button, I do see that the page allows me to go to the next page where I can find my info but the URI changes to the name of the post action that the submit button triggered. for instance if my home login page URI is this

http://localhost/Home/

when clicked on submit the url becomes

http://localhost/home/submit but the view that I load is called AccountInfo not sure how this works.

Also I m storing the user information in a session and there is a logoff button so when I click the button I m setting the session to null and redirecting to the login action, but then my url changes to something like this, though I dont have any url which has a parameter as part of the url in my route.config.

http://localhost/Home/Login?SourceSeqNo=0&SessionSeqNo=0&AsOfDate=4%2F12%2F2016&NumberOfContracts=0&NumberOfPassDueContracts=0

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

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

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

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

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

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

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

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

    }

Action method

    /// <summary>
    /// Logoffs this instance.
    /// </summary>
    /// <returns></returns>
    public ActionResult Logoff()
    {
        Session["AccountInfo"] = null;
        //return View("Login");
        return RedirectToAction("Login", new CustomerModel());   // 
     View("Login", new CustomerModel());
    }

My Submit action is below

   [ValidateAntiForgeryToken]
    public ActionResult Submit(CustomerModel customerModel)
    {
        homeAccountViewModel = new HomeAccountStatusViewModel ();
        homeAccountViewModel.Customer = customerModel;           

        if (ModelState.IsValidField("Username") && 
         (ModelState.IsValidField("Password")))
        {
            logic here....

            return View("AccountStatus", homeAccountViewModel);
        }
        else
        {
            return View("Login", customerModel);
        }            
    }

Any help will be a great favour. Thanks

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
NewTech
  • 316
  • 5
  • 23

1 Answers1

0

When your Action method runs, you are calling:

return RedirectToAction("Login", new CustomerModel());

This will use the outgoing route to build a URL. Outgoing routes use the controller, action, and any route values to find a matching route.

In this case, assuming your controller is Home (since you didn't specify) and you are specifying the action as Register, this route will be matched:

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

And the above route tells the routing engine to build a URL with {controller}/{action}/{id}. In this case ID is optional, so it is left off of the resultant URL.

NOTE: I would be negligent if I didn't point out that your routing is completely misconfigured. The routing framework cannot tell the difference between {controller}/{action}/{id} and {Home}/{Invoice}/{q} for an incoming route. The situation becomes even worse when you have optional values. Both of your first 2 routes will match any URL with 0, 1, 2, or 3 segments. Since the first match always wins, your second (and most of the other routes) will never be reached.

See Why map special routes first before common routes in asp.net mvc? for more information.

Community
  • 1
  • 1
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Thanks for the answer, I played around with my routes and I figured out what you were saying. Since none of my routes have any parameters and all of them are going to be of the same pattern I guess I don need to have so many routes specified, however, there is a little doubt that is bothering me, for a particular route we are saying this is the controller and this is the action then how will it invoke any other action ? I hope you understood what I m trying to say – NewTech Apr 14 '16 at 13:31
  • That is what the default values are for. If you take one or more of the variables out of the URL and use literal segments, the URL cannot change them from the defaults. Have another look at my [linked answer](http://stackoverflow.com/questions/35661062/why-map-special-routes-first-before-common-routes-in-asp-net-mvc/35674633). I have updated it with a list of options for how to fix this type of configuration problem. – NightOwl888 Apr 14 '16 at 16:11