MVC 路由 - URL 在单击提交按钮时更改

MVC Routing - URL changes when submit button is clicked

我是 MVC 的新手,我正在使用 ASP.Net MVC5.I 构建网站,我对路由概念有点困惑。以下是我的 route.config 文件。因此,每当我转到我的主页或登录页面并输入我的凭据并单击提交按钮时,我确实看到该页面允许我转到下一页,在那里我可以找到我的信息,但 URI 更改为post 提交按钮触发的操作。例如,如果我的主页登录页面 URI 是这个

http://localhost/Home/

点击提交后 url 变为

http://localhost/home/submit 但我加载的视图称为 AccountInfo 不确定这是如何工作的。

此外,我将用户信息存储在会话中,并且有一个注销按钮,因此当我单击该按钮时,我将会话设置为空并重定向到登录操作,但随后我的 url 发生了变化像这样的东西,虽然我没有任何 url 在我的 route.config.

中有一个参数作为 url 的一部分

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" }
       );

    }

操作方法

    /// <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());
    }

我的提交操作如下

   [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);
        }            
    }

如有帮助,将不胜感激。谢谢

当您的 Action 方法运行时,您正在调用:

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

这将使用 传出 路由来构建 URL。传出路由使用控制器、操作和任何路由值来查找匹配路由。

在这种情况下,假设您的控制器是 Home(因为您没有指定)并且您将操作指定为 Register,将匹配此路由:

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

而上面的路由告诉路由引擎用{controller}/{action}/{id}构建一个URL。在这种情况下,ID 是可选的,因此它不包含在结果 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 for more information.