对于ASP.NET友好的URL [英] Friendly URLs for ASP.NET
问题描述
Python框架始终提供的方式来处理传达请求的数据在一个优雅的方式,例如像<一个网址href=\"http://somewhere.overtherainbow.com/userid/123424/\">http://somewhere.overtherainbow.com/userid/123424/
Python frameworks always provide ways to handle URLs that convey the data of the request in an elegant way, like for example http://somewhere.overtherainbow.com/userid/123424/
我希望你能注意到结束路径 /用户名/ 123424 /
I want you to notice the ending path /userid/123424/
如何在ASP.NET这样做吗?
How do you do this in ASP.NET?
推荐答案
本例使用ASP.NET路由来实现友好的URL。
This example uses ASP.NET Routing to implement friendly URLs.
映射的例子应用程序句柄是:
Examples of the mappings that the application handles are:
的http:// samplesite /用户名/ 1234 - HTTP://samplesite/users.aspx用户ID = 1234
HTTP:// samplesite /用户名/ 1235 - HTTP://samplesite/users.aspx用户ID = 1235
http://samplesite/userid/1234 - http://samplesite/users.aspx?userid=1234
http://samplesite/userid/1235 - http://samplesite/users.aspx?userid=1235
本例使用查询字符串,并避免任何要求修改code中的aspx页面上。
This example uses querystrings and avoids any requirement to modify the code on the aspx page.
<system.web>
<compilation debug="true">
<assemblies>
…
<add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
…
<httpModules>
…
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
</system.web>
<system.webServer>
…
<modules>
…
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers
…
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</handlers>
</system.webServer>
第2步 - 在Global.asax中
添加路由表定义从友好的URL映射到aspx页面,节约供以后使用要求的用户标识。
Step 2 - add a routing table in global.asax
Define the mapping from the friendly URL to the aspx page, saving the requested userid for later use.
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("UseridRoute", new Route
(
"userid/{userid}",
new CustomRouteHandler("~/users.aspx")
));
}
第3步 - 执行路由处理程序
查询字符串添加到当前环境中的路由发生之前。
Step 3 - implement the route handler
Add the querystring to the current context before the routing takes place.
using System.Web.Compilation;
using System.Web.UI;
using System.Web;
using System.Web.Routing;
public class CustomRouteHandler : IRouteHandler
{
public CustomRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public string VirtualPath { get; private set; }
public IHttpHandler GetHttpHandler(RequestContext
requestContext)
{
// Add the querystring to the URL in the current context
string queryString = "?userid=" + requestContext.RouteData.Values["userid"];
HttpContext.Current.RewritePath(
string.Concat(
VirtualPath,
queryString));
var page = BuildManager.CreateInstanceFromVirtualPath
(VirtualPath, typeof(Page)) as IHttpHandler;
return page;
}
}
$ C $从users.aspx
ç,以供参考aspx页面上的code。
Code from users.aspx
The code on the aspx page for reference.
protected void Page_Load(object sender, EventArgs e)
{
string id = Page.Request.QueryString["userid"];
switch (id)
{
case "1234":
lblUserId.Text = id;
lblUserName.Text = "Bill";
break;
case "1235":
lblUserId.Text = id;
lblUserName.Text = "Claire";
break;
case "1236":
lblUserId.Text = id;
lblUserName.Text = "David";
break;
default:
lblUserId.Text = "0000";
lblUserName.Text = "Unknown";
break;
}
这篇关于对于ASP.NET友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!