简体   繁体   English

从URL中获取用户名,然后使用带有该用户名的查询字符串进行重定向

[英]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. 我想实现一个jQuery脚本,该脚本在域地址中的斜杠“ /”之后使用任何名称,然后使用带有用户名的查询字符串将用户重定向到默认页面。 For example if a user writes mydomain.com/username, I want to redirect the page to mydomain.com/default.aspx?name=username. 例如,如果用户写了mydomain.com/username,我想将页面重定向到mydomain.com/default.aspx?name=username。

How can I accomplish that? 我该怎么做? Thanks 谢谢

SOLUTION

I added the following code in the global.asa file. 我在global.asa文件中添加了以下代码。

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 这是针对URL重写的完整解决方案。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. URL重写是拦截传入的Web请求并将请求重定向到其他资源的过程。 When performing URL rewriting, typically the URL being requested is checked and, based on its value, the request is redirected to a different URL. 执行URL重写时,通常会检查请求的URL,并根据其值将请求重定向到其他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. 例如,网站重组指定目录或文章的网页,并且当通过URL从文章或目录访问网页时,URL会自动移至Blog目录。 That happens by URL rewriting. 这是通过URL重写实现的。

here is a typical example which will work in your specific case.. and let you know how URL rewriting works.. 这是一个典型的示例,将在您的特定情况下工作..让您知道URL重写的工作原理。

add this class in your asp.net website App_code folder 在您的asp.net网站App_code文件夹中添加此类

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" 现在,如果您还没有,请在global.asax中添加以下代码,然后从添加新项中添加它,然后选择“全局应用程序类”

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. 在这里,您可以在default.aspx页面的load事件中检查查询字符串值。

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 本地主机:3030 / WebSite3 / xyz123

if it does not work or having url pattern changed then try to redefine URLRule. 如果它不起作用或更改了网址格式,则尝试重新定义URLRule。 here "xyz123" is name. 这里的“ xyz123”是名字。 I hope it works... 我希望它能起作用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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