locked
how to redirect to some page from a httpmodule RRS feed

  • Question

  • User2097273907 posted

    hello every body !
     i am trying to correct this error/bug what ever call it ?
    i am using asp.net 1.1

    the aim is to transfer the page to some other page from http module i have tried all that i know and all the ways work but the problem is when i request the page seconding time ( without rebuilding ) i get an unexpected results . note that both the all alternatives which i will present here the same problem i.e first time works and second time fails.

    1 . using  server.transfer

    first time works , but second time fails

    the handler for the event BeginRequest is
      void BeginRequestHandler(object sender, EventArgs e)
      {
       char[] CharOnWhichToParse = {'/'};
       String[] requestparts = CurrentContext.Request.Path.Split(CharOnWhichToParse);
       
       //get an object of type CustomSectionOfItimSecurity filled with values with  //respect to values
       //in "system.web/itimSecurity"
       ItimSecuritySectionData temp = (ItimSecuritySectionData)CurrentContext.GetConfig("system.web/itimSecurity"); 

       if (temp.SecurityEnabled.Equals(true))
       {
       
     foreach(Object o in temp.GetPageNames)
        {
       
      String pageName = (String)o;
    if ( pageName.Equals(requestparts[requestparts.Length-1]))
         {
          CurrentContext.Server.Transfer("MyLoginPage.aspx");
                
         }
          
        }
       }
       
      }

    error is "Object reference not set to an instance of an object. "

    line is "CurrentContext.Server.Transfer("MyLoginPage.aspx");"

    2 .

    using CurrentContext.Request.Path.Replace(requestpart[requestparts.Length-1],"MyLoginPage.aspx"); instead of server.transfer
    this time in this method first time works while 2nd time on debugging i found that
    String[] requestparts = CurrentContext.Request.Path.Split(CharOnWhichToParse);

    return requestparts[requestparts.length-1] = "MyLoginPage.aspx"

    in short please post me any working code in httpmodule habndler which transfers to some other page

    Saturday, April 15, 2006 5:32 AM

All replies

  • User1714216230 posted

    Hi jimijani1982,

    Let me help you with your question and then give a suggestion as a work around.

    I was able to get server.transfer to work just fine using this code.  I have 3 pages: default.aspx, login1.aspx, default2.aspx.  When I would browse to default.aspx it would take me to login1.aspx.  Login1.aspx would redirect to default2.aspx.  If I changed the url in the browser's address bar to default.aspx, I would be taken back to login1.aspx.

    public class TransferModule : IHttpModule
    {
    	public TransferModule()
    	{
    		//
    		// TODO: Add constructor logic here
    		//
    	}
    
    	#region IHttpModule Members
    
    	public void Dispose()
    	{
    		throw new Exception("The method or operation is not implemented.");
    	}
    
    	public void Init(HttpApplication context)
    	{
    		context.BeginRequest += new EventHandler(context_BeginRequest);
    	}
    
    	void context_BeginRequest(object sender, EventArgs e)
    	{
    		if(HttpContext.Current.Request.RawUrl.Contains("default.aspx"))
    			HttpContext.Current.Server.Transfer("Login1.aspx");
    	}
    
    	#endregion
    }
    Another idea to protect pages and force a login would be to use the location path of the web.config file.  See this page for details.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/gngrfLocationElement.asp
    Thursday, April 20, 2006 3:18 PM