locked
Unable to Perform Url Rewriting Correctly RRS feed

  • Question

  • User-1363202712 posted
    Hi All.

    I have a very Peculier Problem with Url Rewriting.

    I have Created my HttpHandler class which takes care of URL Rewriting.
    In web.config File I have Declared the HttpHandler Section Like this :

    <httpHandlers>
          <add verb ="*" path ="*.aspx" type="BLL.MyURLRewriteHandler, BLL" />     
    </httpHandlers>

    Here BLL is My project Name and MyURLRewritingHandler is a class file name in that project.

    Now Whenever I write a url for testing URL Rewriting It giives following output In FireFox.


            This XML file does not appear to have any style information associated with it. The document tree is shown below.
         
    -  <html>
    <body/>
    </html>

    I have checked by Debugging and every time Default.aspx page is redirected from My class file as it should be done, but I got this strange Output.

    Awaiting Ur Response
    Help Me ....................


    Tuesday, April 4, 2006 3:52 PM

Answers

  • User-1029435529 posted

    Means I can see the MasterPage but the  respective page doesn't load in the ContentPlaceHolder.
    Unfortunately, I do not have any experience with the master page and content page system in ASP.NET version 2.0, so I cannot answer your question directly.

    However, I recommend that you create for yourself a very simple message log.  Create a utility class that will accept a message string, so that every part of your code can submit messages.  At the end of the page request, the utility class writes all of the messages to a text file. Once you have a message log in place, you can update your master page and content page so that they write messages, which will help you pin-point the problem.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 8, 2006 10:13 PM

All replies

  • User-1029435529 posted
    Now Whenever I write a url for testing URL Rewriting It giives following output In FireFox.
    Please request the same .aspx from Internet Explorer.  Then, select View Source.  Can you paste here the source of the page you receive in IE?
     
    Tuesday, April 4, 2006 9:06 PM
  • User-1363202712 posted
    Hello Sir,

    First of all, Many Thanks for giving the replay.
    Here I list My source code of ProcessRequest from MyHttpHandler.

    It's Logic is if the Second Argument is Admin It will Redirect to the stated page, otherwise lead to the default.aspx page.

    But when I run this, in IE I got a blank page, though there is some content in Default.aspx page.

    Please Help Me....

    One More Request, Iwant to Perform URLRewriting without using Extensions.
    i.e. I want to writ my URL as www.domain.com/software/download/
    for the download page.

    I m developing in ASP.NET 2.0 is there any way I can achive this through HttpHandler without making any changes in IIS or without ISAPI Implementations, Because I don't know C++ Much, and I don't know how to Implement ISAPI Filter.
    Please Give Guidance about this problem with code sample (if Possible).

    Thanking U, in Advance ....

    public void ProcessRequest(HttpContext context)
    {      
                //Remove Querystring from and add later to Default.aspx
                string url = context.Request.Url.ToString().ToLower();

                context.Items.Add("PostUrl", url);
                string queryString = context.Request.QueryString.ToString();
                string newQueryString = "";

                if (queryString.Length > 0)
                {
                    url = url.Replace("?" + queryString, "");
                    newQueryString = "?" + queryString;
                }

                Regex reg = new Regex("(" + context.Request.ApplicationPath.ToLower() + ")");
                string[] splitUrl = reg.Split(url);

                int index = splitUrl[2].ToLower().IndexOf("/admin/");

                if (index == 0)
                {
                    context.RewritePath(context.Request.ApplicationPath + "/" + splitUrl[2].Substring(1));
                }
                else
                {
                    context.Items.Add("key", splitUrl[splitUrl.Length - 1].ToString());

                    context.RewritePath(context.Request.ApplicationPath + "/" + "Default.aspx" +     newQueryString);
                }
    }

    Wednesday, April 5, 2006 6:39 PM
  • User-1029435529 posted

    First, I would not recommend that you use an HttpHandler to do your URL rewriting.  This will mess with your ability to use authentication and authorization to secure your website.  I have always used an HttpModule to do the URL rewriting, and it has always worked without much issue.  I've outlined the HttpModule process in the following thread: URL Rewriting HttpModule.

    Second, removing the extension (such as .aspx) cannot be done without making a change in IIS.  Without making a change in IIS, then a request for /page/without/extension will not get passed to your ASP.NET application, so you simply do not have the chance to handle the request.  You need to add what's known as a wildcard mapping from IIS to ASP.NET.  I've outlined this process in the following thread: URL rewriting and culture codes - Request for Comments ...

    To start with, just get the HttpModule working for pages with an .aspx extension.  Then, once the module is working, you can toy with IIS and remove the extension.

    I hope that these two threads (they're both extensive) put you on the right path.
     

    Wednesday, April 5, 2006 11:31 PM
  • User-1363202712 posted
    Hello Sir,

    Thank u so much for giving me such a good solution.
     
    I have Implemented the HttpModule but I got a very peculier problem this time.

    Though the UrlRewriting works well i.e. if I write /software/download/page.aspx it correctly redirects to the Default.aspx page and the page is shown.

    I have two modules and the other one is "Admin", so if URL consists of /admin/ontentview.aspx then I don't perform rewriting and redirects to the stated page.

    I have used MasterPage in my Project and a treeview control also. Whenever I click On a node of treeViewControl appropriate page should be load in ContentPlaceHolder Pane of MasterPage (TreeView Control is on the Master Page)
    But After implementing UrlRewriting Clicking on the Nodes of TreeView Doesn't generate page.
    i.e. When I click on any node appropriate URL with QueryString is generated and can be seen in address bar, also when i check by debugging the control goes to the page properly, and every line of a page is debugged properly, though I cannot see the page in browser (i.e. in IE, Firefox and Opera)
    Means I can see the MasterPage but the  respective page doesn't load in the ContentPlaceHolder.

    Please give me ome solution, as I just can't understand what is wrong with it.

    My code of UrlRewriting Module.


    //**************************************//

    using System;
    using System.Web;
    using System.Text.RegularExpressions;

    namespace BLL
    {
        public class UrlModule : IHttpModule
        {
            private const string OriginalUrlKey = "UrlModuleRecordOriginalUrl";
            private const string VirtualPathKey = "UrlModuleRecordVirtualPath";

            public void Init(System.Web.HttpApplication Appl)
            {
                Appl.PreRequestHandlerExecute += new System.EventHandler(this.PreRequestHandlerExecute);  
                Appl.BeginRequest+=new System.EventHandler(this.BeginRequest);
                Appl.AuthorizeRequest += new System.EventHandler(this.AuthorizeRequest); 
               
            }
            /// <summary>
            /// required to implement IHttpModule
            /// </summary>
            public void Dispose()
            {
            }

            // Record the original, friendly URL. This value is used later in the Pipeline.

            private void BeginRequest(object sender, EventArgs e)
            {
                HttpContext context = ((HttpApplication)sender).Context;

                //string originalUrl = // code to get original URL from HttpContext.Current.Request
                string originalUrl = HttpContext.Current.Request.Url.ToString().ToLower();  
               
                context.Items[OriginalUrlKey] = originalUrl;

            }

            // Here we rewrite path to handler
            private void AuthorizeRequest(object sender, EventArgs e)
            {
                HttpContext context = ((HttpApplication)sender).Context;

                //string rewrittenUrl = // code to determine the destination handler
                string url                = context.Request.Url.ToString().ToLower();
               
                string rewrittenUrl = GetUrl(context);

                context.Items[VirtualPathKey] = rewrittenUrl;

                context.RewritePath(rewrittenUrl);
            }

            // Here we rewrite the Path of the HttpContext back to the originally requested URL
            private void PreRequestHandlerExecute(object sender, EventArgs e)
            {
                HttpContext context = ((HttpApplication)sender).Context;

                //string originalUrl = (string)context.Items[OriginalUrlKey];
                string originalUrl = (string)context.Items[VirtualPathKey]; 

                context.RewritePath(originalUrl);
                //context.RewritePath("Default.aspx");
            }

            private string GetUrl(HttpContext context)
            {
                //Remove Querystring from and add later to Default.aspx
                string url= context.Request.Url.ToString().ToLower();
                           
                context.Items.Add("PostUrl",url);
                string queryString        = context.Request.QueryString.ToString();
                string newQueryString    = "";

                if (queryString.Length >0 )
                {
                    url                = url.Replace("?" +queryString ,"");
                    newQueryString    = "?" + queryString;
                }
               
                Regex reg            = new Regex("("+ context.Request.ApplicationPath.ToLower()+ ")");
                string[] splitUrl    = reg.Split(url);

                int index = splitUrl[2].ToLower().IndexOf("/admin/");

                if (index == 0)
                {
                    return context.Request.ApplicationPath + "/" + splitUrl[2].Substring(1);
                }
                else
                {
                    context.Items.Add("key", splitUrl[splitUrl.Length - 1].ToString());

                    return context.Request.ApplicationPath + "/" + "default.aspx" + newQueryString;
                }
                //return  context.Request.Url.ToString();
               
                 
            }

        }
       
    }
    //**************************//

    Thanks Again in Advance ......






    Friday, April 7, 2006 10:10 PM
  • User-1029435529 posted

    Means I can see the MasterPage but the  respective page doesn't load in the ContentPlaceHolder.
    Unfortunately, I do not have any experience with the master page and content page system in ASP.NET version 2.0, so I cannot answer your question directly.

    However, I recommend that you create for yourself a very simple message log.  Create a utility class that will accept a message string, so that every part of your code can submit messages.  At the end of the page request, the utility class writes all of the messages to a text file. Once you have a message log in place, you can update your master page and content page so that they write messages, which will help you pin-point the problem.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 8, 2006 10:13 PM