Reverse problem when using Rewrite URL and 404.aspx

I use rewrite URLs on my site to get URLs like:
http://mysite.com/users/john
instead of http://mysite.com/index.aspx?user=john

To achieve this without redistribution using IIS6 and access to the hosting server, I use the “404 approach”. When a request that the server cannot find, a 404-page map is displayed, since it is an aspx page that can be rewritten (I can configure the 404-display using the control panel on the hosting).

This is the code in Global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string url = HttpContext.Current.Request.Url.AbsolutePath;
    if (url.Contains("404.aspx"))
    {
        string[] urlInfo404 = Request.Url.Query.ToString().Split(';');
        if (urlInfo404.Length > 1)
        {
            string requestURL = urlInfo404[1];
            if (requestURL.Contains("/users/"))
            {
                HttpContext.Current.RewritePath("~/index.aspx?user=" + GetPageID(requestURL));              
                StoreRequestURL(requestURL);
            }
            else if (requestURL.Contains("/picture/"))
            {
                HttpContext.Current.RewritePath("~/showPicture.aspx?pictureID=" + GetPageID(requestURL));
                StoreRequestURL(requestURL);
            }
        }
    }
}

private void StoreRequestURL(string url)
{
    url = url.Replace("http://", "");
    url = url.Substring(url.IndexOf("/"));
    HttpContext.Current.Items["VirtualUrl"] = url;
}

private string GetPageID(string requestURL)
{
    int idx = requestURL.LastIndexOf("/");
    string id = requestURL.Substring(idx + 1);
    id = id.Replace(".aspx", ""); //Only needed when testing without the 404-approach
    return id;
}

And in Page_Load on my main page, I set the correct URL in the action attribute in the form tag.

protected void Page_Load(object sender, EventArgs e)
{
    string virtualURL = (string)HttpContext.Current.Items["VirtualUrl"];
    if (!String.IsNullOrEmpty(virtualURL))
    {
        form1.Action = virtualURL;
    }
}

, , postback , - ?

, , 404, , ( ), postback. :
http://mysite.com/users/john.aspx

- , (IIS6, /ISAPI- ).

+3
7
form1.Action = Request.RawUrl

HttpContext.Current.RewritePath("/Default.aspx", true); 

.

Action , ...

!!!

+9

form1.Action = Request.RawUrl , #,

+3

Live Http Headers , Http.

Ahh , , , , :

, ASP.NET Url-Rewriting, . , <form runat="server"> , ASP.NET "" , , . URL-Rewriting , URL-, , URL- (:/products/books), (:/products.aspx?category = ). , , URL- .

ASP.NET 1.0 1.1 <form> , . , - , , , Visual Studio WYSIWYG.

, ASP.NET 2.0 , "" <form>. , ASP.NET 2.0 <form> "" , . - .aspx. .browser /app _browsers, , ""...

+1

:

form1.Action = Request.RawUrl

, #, , VB,

+1

- :

            if (Request.HttpMethod == "GET" && shouldChangeUrl)
            {
                urlRedirect = "REAL-URL-HERE";

                _postBackUrl = urlRedirect;
                Context.RewritePath(urlRedirect);
            }
            else
                //If Post Back (Request.HttpMethod="POST")
                Context.RewritePath(_postBackUrl);

, URL- (: _postBackUrl) URL-, .

+1

404. , ASP.NET 404 .

- , index.aspx? user = john, . SEO , .

0
source

All Articles