Friendly URLs for ASP.NET

后端 未结 6 1084
旧时难觅i
旧时难觅i 2020-11-30 19:48

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/12342

6条回答
  •  醉梦人生
    2020-11-30 20:41

    This example uses ASP.NET Routing to implement friendly URLs.

    Examples of the mappings that the application handles are:

    http://samplesite/userid/1234 - http://samplesite/users.aspx?userid=1234
    http://samplesite/userid/1235 - http://samplesite/users.aspx?userid=1235

    This example uses querystrings and avoids any requirement to modify the code on the aspx page.

    Step 1 - add the necessary entries to web.config

    
    
            
            
        
            
    
    
        
        
        
    
    

    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")
        ));
    }
    

    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;
        }
    }
    

    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;
    }
    

提交回复
热议问题