简体   繁体   中英

url rewriting - build paths

I have started to work on a site without url rewriting. Now, the request is to use friendly urls so i started to use Intelligencia.UrlRewriter.

Let's take an example:

I have a method:

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

which build this kind of urls

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

Now i've added the following rule in web.config:

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

The question is HOW i can make the above method to build that kind of nice url? So no longer return

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

but

"~/Site/Categorii/m1"

without beeing needed to modify its structure?

I mean, i have about 30 methods like the one from the above; it would be extremely helpfull if i can be guided to use a regex at the output of the methods iso modifying the url construction...

Thanks in advance...

you can use something like that

Match mExprStatic = Regex.Match(BaseURL+@"/Site/Categorii/m1", BaseUrl+@"/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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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