ASP.Net URL Encoding

I am implementing URL rewriting in ASP.net and my URLs are causing me problems.

The URL is generated from a database of departments and categories. I want employees to be able to add items to the database with any special characters, without having to break the site.

I encode the data before creating the urls.

There are a few problems ...

  • IIS decodes the URL before it reaches .net, which makes it impossible to correctly parse anything with an "/" in it.
  • ASP.net is confused that URL makes "~" useless on certain pages
  • I switched from the built-in test server to my local IIS server (XP machine), and any URL containing the encoding, and (% 26) gives me a "bad request" error.
  • UrlEncode leaves some breaking characters intact, such as '.'

I had two more related posts on this subject, while I saw only minor problems, not a big problem upstream. I found some tricks in the registry to solve the "bad request" problem, but I'm going to deploy a shared hosting environment, making it useless. I also know that this is a fix for some security issue, so I don’t want to get around it without knowing what might come from the worms that I open.

, .net URL- IIS, URL-.

, AntiXss.URLEncode, HttpUtility.URLEncode, URI.EscapeDataString. , URLEncodng. , , , . , - Hacky % . , , , URL.

- , . , , , , , , - . !


:

, URL- , , .

URLS -

MyStore// + Fridge.aspx
MyStore/Cooking + Equipment.aspx
Mystore/Kitchen/Cutting + Boards.asxpx

, , "Beverage and Bar" "Pastry/Decorating" URL-. , , .

, .

+5
4

, / URL- . URL-. SQL CLR, , , URL- . "Beverage and Bar" "Beverage-And-Bar" "Pastry/Decorating" "Pastry-Decorating". , URL- URL- - . :

public static class URL
{
    static readonly Regex feet = new Regex(@"([0-9]\s?)'([^'])", RegexOptions.Compiled);
    static readonly Regex inch1 = new Regex(@"([0-9]\s?)''", RegexOptions.Compiled);
    static readonly Regex inch2 = new Regex(@"([0-9]\s?)""", RegexOptions.Compiled);
    static readonly Regex num = new Regex(@"#([0-9]+)", RegexOptions.Compiled);
    static readonly Regex dollar = new Regex(@"[$]([0-9]+)", RegexOptions.Compiled);
    static readonly Regex percent = new Regex(@"([0-9]+)%", RegexOptions.Compiled);
    static readonly Regex sep = new Regex(@"[\s_/\\+:.]", RegexOptions.Compiled);
    static readonly Regex empty = new Regex(@"[^-A-Za-z0-9]", RegexOptions.Compiled);
    static readonly Regex extra = new Regex(@"[-]+", RegexOptions.Compiled);

    public static string PrepareURL(string str)
    {
        str = str.Trim().ToLower();
        str = str.Replace("&", "and");

        str = feet.Replace(str, "$1-ft-");
        str = inch1.Replace(str, "$1-in-");
        str = inch2.Replace(str, "$1-in-");
        str = num.Replace(str, "num-$1");

        str = dollar.Replace(str, "$1-dollar-");
        str = percent.Replace(str, "$1-percent-");

        str = sep.Replace(str, "-");

        str = empty.Replace(str, string.Empty);
        str = extra.Replace(str, "-");

        str = str.Trim('-');
        return str;
    }
}

SQL URL . , , URL- . . -, URL-, , , , GoogleBot URL-. , , , . , URL- , .

+4

url rewrite, global.asax , . url, db. aspx, . .

, URL- , , , http GET . , , .

. URL- , .

, , - URL- page.form.action, URL-.

, , , - "-". IIS ". .

P.S. IIS "~", , . , , , , .

Edit:

, IIS, , ./ &. urlencode, IIS - . , :

BeverageBar

/ Pastry Decoration.

URL- , , URL- .

+1

. , . .

. , , , URL-, . , /.

, . , URL-. Server.URLEncode. URL-. IIS URL-, , . +, &, /, !, *, ., ( ). , , , URL-. - , URL-. , . , , , , .

, . . , , VB, # .

Dim strReturn As String = Trim(strStringToEncode)
strReturn = Server.UrlEncode(strReturn)

strReturn = strReturn.Replace("-", "dash").Replace("+", "-")

strReturn = strReturn.Replace("%26", "and").
                    Replace("%2f", "or").
                    Replace("!", "excl").
                    Replace("*", "star").
                    Replace("%27", "apos").
                    Replace("(", "lprn").
                    Replace(")", "rprn").
                    Replace("%3b", "semi").
                    Replace("%3a", "coln").
                    Replace("%40", "at").
                    Replace("%3d", "eq").
                    Replace("%2b", "plus").
                    Replace("%24", "dols").
                    Replace("%25", "pct").
                    Replace("%2c", "coma").
                    Replace("%3f", "query").
                    Replace("%23", "hash").
                    Replace("%5b", "lbrk").
                    Replace("%5d", "rbrk").
                    Replace(".", "dot").
                    Replace("%3e", "gt").
                    Replace("%3c", "lt")

Return strReturn
+1

, HttpUtility.UrlEncode HttpUtility.HtmlDecode

string url = "http://www.google.com/search?q=" + HttpUtility.UrlEncode("Example");
0