Global.asax and Url rewriting

Discussion in 'ASP.NET 2.0' started by raymondp, Feb 26, 2008.

  1. Try opening a ticket to discountasp support. And request for wild card mapping to the ASPnet ISAPI DLL. I believe you still need to incorporate wild card mapping for URL rewriting to work properly.

    rcp
    DiscountASP.NET
    www.DiscountASP.NET
     
  2. Hello,

    I have the following global.asax in the www-root "winfaktenco" of my application:

    "<script language=C# runat="server">
    void Application_BeginRequest (Object sender, EventArgs e)
    {
    //
    // TODO: Convert a path of the form
    // .../dok1.html into a path of the form
    // .../dok.aspx?dok_id=1
    //
    HttpContext context = HttpContext.Current;
    string oldpath = context.Request.Path.ToLower ();

    string token = "/dok";

    int i = oldpath.IndexOf (token);
    int len = token.Length;

    if (i != -1) {
    int j = oldpath.IndexOf (".html");
    if (j != -1) {
    string id =
    oldpath.Substring (i + len, j - (i + len));
    string newpath =
    oldpath.Replace (token + id + ".html",
    "/dok.aspx?dok_id=" + id);
    context.RewritePath (newpath);
    }
    }

    token = "/book";

    i = oldpath.IndexOf (token);
    len = token.Length;
    if (i != -1) {
    int j = oldpath.IndexOf (".html");
    if (j != -1) {
    string id = oldpath.Substring (i + len, j - (i + len));
    string newpath = oldpath.Replace (token + id + ".html","/book.aspx?book_id=" + id);
    context.RewritePath (newpath);
    }
    }
    token = "/chapter";

    i = oldpath.IndexOf (token);
    len = token.Length;
    if (i != -1) {
    int j = oldpath.IndexOf (".html");
    if (j != -1) {
    string id = oldpath.Substring (i + len, j - (i + len));
    string newpath = oldpath.Replace (token + id + ".html","/chapter.aspx?chapter_id=" + id);
    context.RewritePath (newpath);
    }
    }
    token = "/topic";

    i = oldpath.IndexOf (token);
    len = token.Length;
    if (i != -1) {
    int j = oldpath.IndexOf (".html");
    if (j != -1) {
    string id = oldpath.Substring (i + len, j - (i + len));
    string newpath = oldpath.Replace (token + id + ".html","/topic.aspx?topic_id=" + id);
    context.RewritePath (newpath);
    }
    }
    }
    </script>"

    It seems that the global.asax is ignored by the server. Every time I'm calling links like "http://www.winfakten.com/dok36.html" the Url rewriting does not happen.

    Please help me.

    Kind regards,

    Bernd Ebersberger
     
  3. Hi all,

    I am getting the same behavior with my rewrite. When I try it on my development environment, it works fine. But when I try the following in my hosted environment's global.asax Application_BeginRequest, I get an error.

    if (Request.Url.AbsoluteUri.Contains("/es/"))
    {
    Response.Cookies["Lang"].Value = "es-UY";
    string _newPath = Request.Url.AbsolutePath.Replace("/es/", "/");
    Context.RewritePath(_newPath, false);
    }



    Error is below:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------
    The resource cannot be found.
    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
    Requested URL: /es/default.aspx

    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433

    Any idea what is happening and more importantly how to fix it?

    Here is the link if you want to try it yourselves. http://www.horizontesunidos.com/es/default.aspx

    Thanks,
    JE

    Post Edited (digitalje) : 3/4/2008 3:49:11 AM GMT
     
  4. digitalje,

    It looks like IIS is trying to find the document at /es/default.aspx and can't find it, so it returns 404 without ever running your rewrite logic. There is a setting in the IIS6 file mappings that tells it whether or not to verify that a document exists before running the mapped handler (in your case the ASP.NET ISAPI DLL). Probably in your development environment, IIS is not checking to verify the document exists, but on the DASP server it is. You should open a ticket with support to have them check that setting for you. IIS won't ever run your rewrite code if it's trying to verify the un-rewritten URL is valid first. Hope that helps.

    jdc
     
  5. Excellent [​IMG]
     
  6. Hi all,

    I wanted to thank everybody for your help. I found the solution to my problem and wanted to post it in case someone else experiences the issue. After spending sometime on google, I found a few articles and posts indicating that precompiling an application in Visual Studio will not copy the global.asax file to the precompile folder. The articles recommended that you copy the file to the folder. I tried it and it worked. I hadn't realized the file was missing.

    Hope this helps someone in the future.

    JE
     
  7. Hi all,

    I am back again. I thought I had this working, but found another problem...Boo-hoo...

    I uploaded a sitemap to Google with my new "virtual" url and waited to see how the Google bot would handle the path. It came through last night and reported an HTTP 500 error (Internal server error) on the url. Now the thing that I don't understand is that I can see the page perfectly fine in my browser. What is different between the bot and the browser request...I am missing something here. Can someone please enlighten me?

    Below is the URL:

    http://www.horizontesunidos.com/es/default.aspx

    Its almost like the request is not being passed to the ASP.NET service when the request is made by the bot.

    Thanks,
    Jennifer
     

Share This Page