2010-06-05 222 views
1

我已經開始在沒有url重寫的網站上工作。現在,請求是使用友好的網址,所以我開始使用Intelligencia.UrlRewriter。url重寫 - 構建路徑

讓我們舉個例子:

我有一個方法:

public static string getCategoriesIndexLink(string category) 
{ 
    string baseUrl = getBaseUrl() + (String)HttpContext.GetGlobalResourceObject("Paths", "categorii.index"); 
    return baseUrl.AddQueryParam(CQueryStringParameters.CATEGORY, category); 
} 

其打造這種URL的

"~/Site/Categorii.aspx?category=$1" 

現在我已在web.config中的以下規則:

<rewrite url="~/Site/Categorii/(.+)" to="~/Site/Categorii.aspx?category=$1" /> 

問題是如何使上述方法建立這種漂亮的網址? 因此不再返回

"~/Site/Categorii.aspx?category=m1" 

"~/Site/Categorii/m1" 

沒有beeing需要修改它的結構?

我的意思是,我有大約30種方法,如上面的一個;這將是非常有益的,如果我可以在引導方法ISO修改URL建設的輸出端使用正則表達式...

在此先感謝...

回答

0

你可以使用類似的東西

Match mExprStatic = Regex.Match([email protected]"/Site/Categorii/m1", [email protected]"/site/categorii/(?<category>\S*)", RegexOptions.IgnoreCase | RegexOptions.Singleline); 
if (mExprStatic.Success || !string.IsNullOrEmpty(mExprStatic.Value)) 
{ 
    string parameters = "?" + mExprStatic.Groups["category"].Value; 
    if ((context.Request.QueryString.AllKeys != null) && (context.Request.QueryString.AllKeys.Length > 0)) 
    { 
     foreach (string key in Request.QueryString.AllKeys) 
       parameters += "&" + key + "=" + Request[key]; 
    } 
    Server.Transfer("~/Site/Categorii.aspx" + parameters , false);