简体   繁体   中英

Take the user name from the URL and redirect with a query string with that user name

I want to implement a jquery script that takes any name after the slash "/" in a domain address and redirect the user to the default page with a querystring with the user name. For example if a user writes mydomain.com/username, I want to redirect the page to mydomain.com/default.aspx?name=username.

How can I accomplish that? Thanks

SOLUTION

I added the following code in the global.asa file.

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        Uri uriAddress = new Uri(Request.Url.AbsoluteUri);

        if (uriAddress.Segments != null && uriAddress.Segments.Length > 1 && !String.IsNullOrEmpty(uriAddress.Segments[1]))
        {
            string SegmentUsername = uriAddress.Segments[1];
            Response.Redirect("default.aspx?name=" + SegmentUsername);                
        } 
    }

here it is a complete solution against URL Rewriting.. http://www.codeproject.com/Articles/641758/An-Example-of-URL-Rewriting-With-ASP-NET

text from there.. URL rewriting is the process of intercepting an incoming Web request and redirecting the request to a different resource. When performing URL rewriting, typically the URL being requested is checked and, based on its value, the request is redirected to a different URL. For example, a website restructuring web pages of a specified directory or article and when accessing a web page from an article or directory by URL then the URL is automatically moved on to the Blog directory. That happens by URL rewriting.

here is a typical example which will work in your specific case.. and let you know how URL rewriting works..

add this class in your asp.net website App_code folder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for Class1
/// </summary>
 public class URLRule
    {
        public string URLPateren { set; get; }
        public string Rewrite { set; get; }
    }
    public class ListURL : List<URLRule>
    {

        public ListURL()
        {
            //may be you need to redefine this rule in order to make it mature.
            URLRule obj = new URLRule();
            obj.URLPateren = "/(.*)?/(.*)";
            obj.Rewrite = "default.aspx?name=$2";
            Add(obj);
            //here you can add more rules as above..

        }

        public string Process(string str)
        {

            Regex oReg;

            foreach (URLRule obj in this)
            {
                oReg = new Regex(obj.URLPateren);
                Match oMatch = oReg.Match(str);

                if (oMatch.Success)
                {
                    string s = oReg.Replace(str, obj.Rewrite);
                    return s;
                }
            }
            return str;
        }
    }

now add this following piece of code in your global.asax if you do not already have then add it from adding new item then select "global application class"

protected void Application_BeginRequest(object sender, EventArgs e)
    {



        ListURL rewriter = new ListURL();

        string re = rewriter.Process(Request.Path);
        if (Request.Path != re)
        {
            HttpContext.Current.RewritePath(re);
        }



    }

and here you can check your query string value at default.aspx page's load event.

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.HasKeys()) {
            string queryvalue = Request.QueryString["name"];
            Response.Write("User Name : " + queryvalue);

        }
    }

i tried this url and working fine..

localhost:3030/WebSite3/xyz123

if it does not work or having url pattern changed then try to redefine URLRule. here "xyz123" is name. I hope it works...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM