簡體   English   中英

從URL中獲取用戶名,然后使用帶有該用戶名的查詢字符串進行重定向

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

我想實現一個jQuery腳本,該腳本在域地址中的斜杠“ /”之后使用任何名稱,然后使用帶有用戶名的查詢字符串將用戶重定向到默認頁面。 例如,如果用戶寫了mydomain.com/username,我想將頁面重定向到mydomain.com/default.aspx?name=username。

我該怎么做? 謝謝

我在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);                
        } 
    }

這是針對URL重寫的完整解決方案。http://www.codeproject.com/Articles/641758/An-Example-of-URL-Rewriting-With-ASP-NET

URL重寫是攔截傳入的Web請求並將請求重定向到其他資源的過程。 執行URL重寫時,通常會檢查請求的URL,並根據其值將請求重定向到其他URL。 例如,網站重組指定目錄或文章的網頁,並且當通過URL從文章或目錄訪問網頁時,URL會自動移至Blog目錄。 這是通過URL重寫實現的。

這是一個典型的示例,將在您的特定情況下工作..讓您知道URL重寫的工作原理。

在您的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;
        }
    }

現在,如果您還沒有,請在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);
        }



    }

在這里,您可以在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);

        }
    }

我嘗試了這個網址並正常工作..

本地主機:3030 / WebSite3 / xyz123

如果它不起作用或更改了網址格式,則嘗試重新定義URLRule。 這里的“ xyz123”是名字。 我希望它能起作用...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM