2010-06-05 162 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);