kri*_*ian 13 c# asp.net url url-routing
我读过ASP.NET Routing ... Goodbye URL重写?和使用路由使用WebForms这些都是很棒的文章,但仅限于简单的,说明性的,"hello world" - 复杂的例子.
是否有人以非平凡的方式使用ASP.NET路由与Web表单?任何需要注意的问题?性能问题?进一步推荐阅读我应该先看看我自己的实现?
编辑 找到这些额外有用的URL:
小智 5
ASP.NET中如何使用路由的简单例子
添加到 default.aspx 3 个按钮 -
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");
}
Run Code Online (Sandbox Code Playgroud)读取第三页上的查询字符串
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["Name"]);
}
Run Code Online (Sandbox Code Playgroud)现在,如果您运行该程序,您将能够导航到第二种和第三种形式。以前是这样的。让我们添加路由。
添加新项目 - Global.aspx 使用 System.Web.Routing;
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"
);
}
Run Code Online (Sandbox Code Playgroud)在 default.aspx 中修改 protected void Button1_Click(object sender, EventArgs e) { // Response.Redirect("Second.aspx"); Response.Redirect(GetRouteUrl("SecondRoute", null)); }
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" }));
}
Run Code Online (Sandbox Code Playgroud)修改third.aspx中的页面加载
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write(Request.QueryString["Name"]);
Response.Write(RouteData.Values["Name"]);
}
Run Code Online (Sandbox Code Playgroud)运行程序,请注意 url 看起来更干净 - 里面没有文件扩展名(Second.aspx 变成了 Second)
传递多个参数
使用以下代码向 default.aspx 添加新按钮:
protected void Button4_Click(object sender, EventArgs e)
{
Response.Redirect(GetRouteUrl("FourthRoute", new { Name = "Shoes" , Gender = "Male"}));
}
Run Code Online (Sandbox Code Playgroud)将以下代码添加到 global.asax
routes.MapPageRoute(
"FourthRoute",
"Fourth/{Name}-{Gender}",
"~/Fourth.aspx"
);
Run Code Online (Sandbox Code Playgroud)创建具有以下页面加载的 Four.aspx 页面:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Name is: " + RouteData.Values["Name"] + " and Gender is " + RouteData.Values["Gender"]);
}
Run Code Online (Sandbox Code Playgroud)不确定这是否是您的答案,但这可能会让您找到正确的方向,Scott Hanselman (MSFT) 展示了如何让 ASP.NET WebForms、ASP.NET MVC 和 ASP.NET 动态数据 — 哦,还有 AJAX 在和谐。
| 归档时间: |
|
| 查看次数: |
11340 次 |
| 最近记录: |