Python框架总是提供处理以优雅方式传达请求数据的URL的方法,例如http://somewhere.overtherainbow.com/userid/123424/
我希望你注意结束路径/ userid/123424 /
你是如何在ASP.NET中做到这一点的?
此示例使用ASP.NET路由实现友好URL.
应用程序处理的映射示例如下:
http:// samplesite/userid/1234 - http://samplesite/users.aspx?userid = 1234
http:// samplesite/userid/1235 - http://samplesite/users.aspx?userid = 1235
此示例使用查询字符串,并避免修改aspx页面上的代码的任何要求.
第1步 - 将必要的条目添加到web.config第2步 - 在global.asax中添加路由表… … … … …
定义从友好URL到aspx页面的映射,保存请求的用户ID以供以后使用.
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步 - 实现路由处理程序
在路由发生之前将查询字符串添加到当前上下文.
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; } }来自users.aspx的代码
aspx页面上的代码供参考.
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的示例.
应用程序处理的映射示例如下:
http:// samplesite/userid/1234 - http://samplesite/users.aspx?userid = 1234
http:// samplesite/userid/1235 - http://samplesite/users.aspx?userid = 1235
此示例不使用查询字符串,但需要在aspx页面上添加其他代码.
第1步 - 将必要的条目添加到web.config第2步 - 在global.asax中添加路由表… … … … …
定义从友好URL到aspx页面的映射,保存请求的用户ID以供以后使用.
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步 - 实现路由处理程序
将包含参数的路由上下文传递给页面.(注意IRoutablePage的定义)
using System.Web.Compilation; using System.Web.UI; using System.Web; using System.Web.Routing; public interface IRoutablePage { RequestContext RequestContext { set; } } public class CustomRouteHandler : IRouteHandler { public CustomRouteHandler(string virtualPath) { this.VirtualPath = virtualPath; } public string VirtualPath { get; private set; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { var page = BuildManager.CreateInstanceFromVirtualPath (VirtualPath, typeof(Page)) as IHttpHandler; if (page != null) { var routablePage = page as IRoutablePage; if (routablePage != null) routablePage.RequestContext = requestContext; } return page; } }步骤4 - 在目标页面上检索参数
请注意IRoutablePage的实现.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Routing; public partial class users : System.Web.UI.Page, IRoutablePage { protected RequestContext requestContext; protected object RouteValue(string key) { return requestContext.RouteData.Values[key]; } protected void Page_Load(object sender, EventArgs e) { string id = RouteValue("userid").ToString(); 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; } } #region IRoutablePage Members public RequestContext RequestContext { set { requestContext = value; } } #endregion }