這裏是一個典型的例子,這將在您的特定情況下工作.. ,讓你知道如何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頁面的加載事件中檢查您的查詢字符串值。
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
,如果它不能正常工作或然後有URL格局未變嘗試重新定義URLRule。 這裏「xyz123」是名字。 我希望它能正常工作...
你應該在服務器端做這件事(解析URL,重定向等)。 JS和jQuery與描述的情況無關。 – Regent 2014-10-28 09:27:11
我認爲從服務器端做到這一點是最好的做法..可能你需要做的URL重寫.. – 2014-10-28 09:28:48