Search code examples
urlhttpmodule

http module cant get correct page url


Hi i got a very noob question to ask . I am using http module to do a access right. Let say the user is 'admin' then he got authorized to view the page.The http module will get the access right from the database based on the page url, thereafter the http module will determine the user is allowed to access or not .

Here is my sample coding :

 public void Init(HttpApplication context)
    {
        context.AcquireRequestState += new EventHandler(context_AcquireRequestState1);

    }



    void context_AcquireRequestState1(object sender, EventArgs e)
    {
        try
        {

           string requestUrl = application.Request.AppRelativeCurrentExecutionFilePath.ToString().Trim();
            //return last string of .aspx
         string   requestAspx = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1).Trim();

    }

but the httpmodule will run several time. It cant get the url correctly. For example first time it may get ~/Module/Admin/Role/RoleManagementList.aspx. then second time will get the wrong url ~/favicon.ico.Can anyone help me solve this problem? thank you so much


Solution

  • You are not getting the "wrong" url. The user's browser is simply making a different request for a different resource. You http module will execute for each http request, which will mean one for each resource in addition to the "page" such as favicons (displayed in the browsers url, and sometimes requested even if you don't have one) or images, external .css, external .js files, etc. referenced on the page (unless they are directly served by IIS bypassing the ASP.NET stack). You will need to consider all of these urls in your module.

    Depending on how tightly you control your deployment environment,you may also/instead be able to exempt certain file extensions from every hitting asp.net by having IIS simply serve them directly. See http://msdn.microsoft.com/en-us/library/ms972953.aspx