有没有人使用ASP。用简单的方式使用web表单进行网络路由?有什么事情需要注意吗?有没有可能遇到性能问题?
发布于 2017-12-27 12:32:44
您可以在下面的文章中找到以简单方式解释的URL路由。它提供了如下信息:在路由上发送请求,在目标页面上检索URL参数,设置参数的默认值。
发布于 2017-12-27 13:17:20
关于如何在ASP中使用路由的简单示例。网
protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("Second.aspx"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("Third.aspx?Name=Pants"); } protected void Button3_Click(object sender, EventArgs e) { Response.Redirect("Third.aspx?Name=Shoes"); }
protected void Page_Load(object sender, EventArgs e) { Response.Write(Request.QueryString["Name"]); }
现在,如果您运行该程序,您将能够导航到五表单。以前是这样的。让我们加上路由。protected void Application_Start(object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); } void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute( "HomeRoute", "Home", "~/Default.aspx" ); routes.MapPageRoute( "SecondRoute", "Second", "~/Second.aspx" ); routes.MapPageRoute( "ThirdRoute", "Third/{Name}", "~/Third.aspx" ); }
protected void Button2_Click(object sender, EventArgs e) { //Response.Redirect("Third.aspx?Name=Pants"); Response.Redirect(GetRouteUrl("ThirdRoute", new {Name = "Pants"})); } protected void Button3_Click(object sender, EventArgs e) { // Response.Redirect("Third.aspx?Name=Shoes"); Response.Redirect(GetRouteUrl("ThirdRoute", new { Name = "Shoes" })); }
protected void Page_Load(object sender, EventArgs e) { //Response.Write(Request.QueryString["Name"]); Response.Write(RouteData.Values["Name"]); }
运行程序,请注意,url看起来要干净得多--其中没有文件扩展名(Secd.aspx仅为第二位)
protected void Button4_Click(object sender, EventArgs e) { Response.Redirect(GetRouteUrl("FourthRoute", new { Name = "Shoes" , Gender = "Male"})); }
- add the following code to global.asax
`routes.MapPageRoute( "FourthRoute", "Fourth/{Name}-{Gender}", "~/Fourth.aspx" );`
- create Fourth.aspx page with the following page load:
protected void Page_Load(object sender, EventArgs e) { Response.Write("Name is: " + RouteData.Values["Name"] + " and Gender is " + RouteData.Values["Gender"]); }
https://stackoverflow.com/questions/-100000039
复制相似问题