简体   繁体   English

Web表单中的路由不起作用

[英]Routing in Web forms not working

This is the code in Global.asax 这是Global.asax中的代码

void Application_Start(object sender, EventArgs e)
{
System.Web.Routing.RouteTable.Routes.MapPageRoute("Page1","{Param1}/{Param2}","~/FirstPage.aspx");

System.Web.Routing.RouteTable.Routes.MapPageRoute("Page2","Xyz/{Param3}","~/Second.aspx");   
}

Now i'm trying to redirect to some page eg xyz/test1,xyz/test2,xyz/test3. 现在,我正在尝试重定向到某个页面,例如xyz / test1,xyz / test2,xyz / test3。 It's always getting redirected to FirstPage, not the second page which is what is expected and i'm trying to do. 它总是被重定向到FirstPage,而不是第二页,这是预期的,我正在尝试做。

How to implement this? 如何实现呢? Any suggestions. 有什么建议么。

Please make sure below important things are met before you start creating routing, 在开始创建路由之前,请确保满足以下重要条件,

•Define custom URL patterns that are not dependent on physical file names. •定义不依赖于物理文件名的自定义URL模式。 •Generate URLs based on route URL parameter values by using markup or code. •使用标记或代码,根据路线URL参数值生成URL。 •In a routed page, retrieve values passed in URL segments by using markup or code. •在路由页面中,通过使用标记或代码来检索URL段中传递的值。

Try below code to implement routing, 1. In Application_start method you can only register the routes as below, 尝试使用以下代码来实现路由:1.在Application_start方法中,您只能按以下方式注册路由,

 void Application_Start(object sender, EventArgs e)
 {
     RegisterRoutes(RouteTable.Routes);
 }
  1. Create below method after Session_End method in you global.asax file, void RegisterRoutes(RouteCollection routes) { } 在您global.asax文件中的Session_End方法之后创建以下方法,void RegisterRoutes(RouteCollection route){}

  2. In the RegisterRoutes method, add the following code, 在RegisterRoutes方法中,添加以下代码,

    routes.MapPageRoute("SalesRoute", "SalesReport/{locale}/{year}", "~/sales.aspx"); route.MapPageRoute(“ SalesRoute”,“ SalesReport / {locale} / {year}”,“〜/ sales.aspx”);

This code adds a route that is named SalesRoute. 此代码添加了一个名为SalesRoute的路由。 You are naming this route because it has the same parameter list as the route that you will create in the following step. 您正在命名此路由,因为它与在下一步中将创建的路由具有相同的参数列表。 Assigning names to these two routes enables you to differentiate them when you generate URLs for them. 为这两个路由分配名称可以使您在为其生成URL时区分它们。

More details refer: https://msdn.microsoft.com/en-us/library/dd329551.aspx 有关更多详细信息,请参见: https : //msdn.microsoft.com/en-us/library/dd329551.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM