ASP.NET WebPages路由

时间:2013-04-11 05:08:38

标签: asp.net asp.net-mvc

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

拥有文件夹帐户,以及Register.cshtml,LogIn等许多文件...... 并且网址:localhost / Account / Register

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

试图在_AppStart.cshtml中映射它:

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

它不起作用。

2 个答案:

答案 0 :(得分: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" });

答案 1 :(得分: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 }
                );