0

我创建了简单的 asp.net 项目。

有文件夹帐户,以及许多文件,如 Register.cshtml、LogIn 等...和 ​​url:localhost/Account/Register

希望它是:localhost/Register,localhost/LogIn。

尝试在 _AppStart.cshtml 中映射它:

RouteTable.Routes.MapPageRoute("Account", "{Action}", "~/Account/{Action}"); 

它不起作用。

4

2 回答 2

0

试试这个....

 //Default 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default",
                "", 
                new { controller = "Home", action = "Index", id = "" }
            );

          //url rewriting

            RouteTable.Routes.MapRoute(null, "Register/{ID}", new { controller = "Home", action = "Register" });
于 2013-04-11T05:29:02.243 回答
0

首先,您必须使用 MapRoute 到 MapPageRoute,因为 MapPageRoute 是(来自msdn):

提供一种为 Web 窗体应用程序定义路由的方法

但是,如果您像这样修改代码:

     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

     routes.MapRoute("AccountRoute", "{Action}", new {controller = "Account"});

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

每个没有参数的请求都将转到 AccountRoute,即使它应该由其他控制器处理。

唯一可行的方法是创建如下代码:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

//ALL routes you need
routes.MapRoute("LoginRoute", "{Action}", new { controller = "Account", action = "Login" });
routes.MapRoute("RegisterRoute", "{Action}", new { controller = "Account", action = "Register" });            

//Default route in the end
routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
于 2013-04-11T05:41:58.270 回答