locked
302 is sent back to browser when response.redirect is used. can it be manupulated RRS feed

Answers

  • User475983607 posted

    Code copied and slightly modified from the link provided openly above; https://docs.microsoft.com/en-us/previous-versions/ms227673(v=vs.140)?redirectedfrom=MSDN

    using System;
    using System.Web;
    
    namespace WebFormsDemo.HttpModulesHandelers
    {
        public class CustomRedirectModule : IHttpModule
        {
            public string ModuleName { get; } = "Custom Redirect Module";
    
            public void Init(HttpApplication application)
            {
                application.BeginRequest += Application_BeginRequest;
                application.EndRequest += Application_EndRequest;
            }
    
            public void Application_BeginRequest(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;     
                //Stub
            }
    
            public void Application_EndRequest(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
    
                //Check for a 302 response where the location header starts with "/default.aspx"
                if(context.Response.StatusCode == 302 & 
                    context.Response.Headers["location"] != null && context.Response.Headers["location"].StartsWith("/default.aspx"))
                {
                    context.Response.Headers["location"] = "/kkk/default.aspx";
                }
            }
    
            public void Dispose()
            {
                //Dispose 
            }
        }
    }
      <system.webServer>
        <modules>
          <add name="CustomRedirectModule" type="WebFormsDemo.HttpModulesHandelers.CustomRedirectModule"/>
        </modules>
      </system.webServer>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2020 2:34 PM

All replies

  • User475983607 posted

    i have seen handlers and modules can be written to hook up in the life cycle of asp.net pipeline.

    so when i noticed that response.redirect(~/Default.aspx) sends back

    302 found to the browser and then browser executes GET for the url http://localhost:81/Default.aspx

    then I realised that it is possible to put some path to in http://localhost;81/kkk/default.aspx 

    by intercepting the 302 found  http://localhost:81/Default.aspx and changing it to  http://localhost;81/kkk/default.aspx 

    so that when browser calls to should call the new one.

    Why don't you simply add Response.Redirect(~/kkk/Default.aspx) to the Page_Load event of /Default.aspx page?

    You can get into the HTTP pipeline using HTTP Modules which allows you to change the response.

    https://docs.microsoft.com/en-us/previous-versions/ms227673(v=vs.140)?redirectedfrom=MSDN

    I you explain why you have this problem then the community might be able to provide an alternative solution.

    Friday, May 1, 2020 12:56 PM
  • User2102072086 posted

    it writes content to the page, that we have done already. but we were not sure about the 302 response URL manipulation.

    so I want to change the 302  URL.  any example would be helpful.

    Saturday, May 2, 2020 8:29 AM
  • User475983607 posted

    it writes content to the page, that we have done already. but we were not sure about the 302 response URL manipulation.

    so I want to change the 302  URL.  any example would be helpful.

    Did you read the link?  The link contains source code that specifically illustrates where to modify the response.  Did you try writing code?  Can you share what you tried?  Anything?

    Again, your solution seems odd and it points to a general design bug.  Can you explain how you got your self into this situation?  Seems to me you can just do a find and replace to fix the Response.Redirect("~/default.aspx")

    Saturday, May 2, 2020 10:31 AM
  • User753101303 posted

    Hi,

    In your  other thread I gave a link for using the url rewriting module as it seems what you want.

    You don't have the source code? Explain maybe your constraints but for now it seems that for some unknown reason you are making that harder than it should.

    Edit: and you also talked about Server.Transfer ??? IMO explain first which problem you are trying to solve. You want just to change the location of a single page within your app ?

    Saturday, May 2, 2020 10:42 AM
  • User2102072086 posted

    if i had time to research i would not have asked here, as far as the  module writing is concerned it is on internet, there is know need to ask.

    if u focuse what i am asking then only we will be able to answer.

    kindly read my first post.

    Saturday, May 2, 2020 12:09 PM
  • User475983607 posted

    rajemessage

    if i had time to research i would not have asked here, as far as the  module writing is concerned it is on internet, there is know need to ask.

    if u focuse what i am asking then only we will be able to answer.

    kindly read my first post.

    I think if you showed the slightest effort to write code, forum member would be more willing to provide coding assistance.  Frankly, stating that you do not have time to research does not help convince form members to use their precious time to write your code.  Good luck.  

    Saturday, May 2, 2020 1:05 PM
  • User475983607 posted

    Code copied and slightly modified from the link provided openly above; https://docs.microsoft.com/en-us/previous-versions/ms227673(v=vs.140)?redirectedfrom=MSDN

    using System;
    using System.Web;
    
    namespace WebFormsDemo.HttpModulesHandelers
    {
        public class CustomRedirectModule : IHttpModule
        {
            public string ModuleName { get; } = "Custom Redirect Module";
    
            public void Init(HttpApplication application)
            {
                application.BeginRequest += Application_BeginRequest;
                application.EndRequest += Application_EndRequest;
            }
    
            public void Application_BeginRequest(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;     
                //Stub
            }
    
            public void Application_EndRequest(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
    
                //Check for a 302 response where the location header starts with "/default.aspx"
                if(context.Response.StatusCode == 302 & 
                    context.Response.Headers["location"] != null && context.Response.Headers["location"].StartsWith("/default.aspx"))
                {
                    context.Response.Headers["location"] = "/kkk/default.aspx";
                }
            }
    
            public void Dispose()
            {
                //Dispose 
            }
        }
    }
      <system.webServer>
        <modules>
          <add name="CustomRedirectModule" type="WebFormsDemo.HttpModulesHandelers.CustomRedirectModule"/>
        </modules>
      </system.webServer>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 2, 2020 2:34 PM
  • User753101303 posted

    Or in the other thread I posted a link https://www.jondjones.com/architecture/continuous-intergration/iis/ultimate-guide-to-creating-url-rewrite-rules-within-iis/ the first example showing how to use the url rewriting module. It's best to avoid posting the same question multiple times which makes things harder to follow.

    Note also that it means that you'll have two redirection when you could have one instead. Make sure you have a strong reason for not just fixing the source issue.

    Sunday, May 3, 2020 11:29 AM
  • User2102072086 posted

    this was working, but i noticed that by justputting

    following in global.asax it was wokring so is it required to make an handeler?

    public void Application_EndRequest(object source, EventArgs e)
            {
                HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
    
                //Check for a 302 response where the location header starts with "/default.aspx"
                if (context.Response.StatusCode == 302 &
                    context.Response.Headers["location"] != null )
                {
                    if (context.Response.Headers["location"].StartsWith("/xyz.aspx"))
                    {
                        context.Response.Headers["location"] = "/ptk/xyx8016/xyz.aspx";
                    }
                    else
                    {
                        context.Response.Headers["location"] = "/ptk/abc8016/abc.aspx";
                    }
    
    
                }
            }

    Tuesday, May 5, 2020 5:06 AM
  • User753101303 posted

    Do yo mean it is NOT working? A problem could be that Response.Redirect aborts the thread by default (causing likely this event to be stkipped) and fixing this would require anyway to change existing Response.Redirect calls.

    Tuesday, May 5, 2020 7:17 AM
  • User2102072086 posted

    it is working, thank you very much.

    but I have not made handler, instead, I added the event in global.asmx.

    and it is working. so my question is, what I did is correct or should I make a handler?

    Tuesday, May 5, 2020 8:48 AM
  • User753101303 posted

    An http handler is to process http requests in a particular way (for example ASPX pages).

    It would be rather an http module as shown ealier (or the url rewriting module already mentionned). If it works already rather than spending time in a mdule I would spent my time in updating my application code (maybe adding a configurartion value if you plan to change this later) so it works as I want to start with ;-)

    Tuesday, May 5, 2020 9:57 AM
  • User475983607 posted

    it is working, thank you very much.

    but I have not made handler, instead, I added the event in global.asmx.

    and it is working. so my question is, what I did is correct or should I make a handler?

    Please read your original post.  You asked for an HTTP Handler or Module. 

    The Global.asax also works.

    Tuesday, May 5, 2020 10:33 AM
  • User2102072086 posted

    I wrote handler word in a general way, u should pick up from context. what we were talking about.

    by the way if u should see what was given to me  WebFormsDemo.HttpModulesHandelers.CustomRedirectModule

    but u should stick to the first line.

    and my question is not answered, should I use global.asmx or should I write a module the way u have written.

    type="WebFormsDemo.HttpModulesHandelers.CustomRedirectModule"/>
    Tuesday, May 5, 2020 11:45 AM
  • User475983607 posted

    rajemessage

    I wrote handler word in a general way, u should pick up from context. what we were talking about.

    by the way if u should see what was given to me  WebFormsDemo.HttpModulesHandelers.CustomRedirectModule

    but u should stick to the first line.

    and my question is not answered, should I use global.asmx or should I write a module the way u have written.

    type="WebFormsDemo.HttpModulesHandelers.CustomRedirectModule"/>

    It's the Global.asax not asmx.  Asmx is a web service. 

    Either approach works.  It is up to you to pick an approach that meets your application requirements.   I would use a module as is seems like the approach is more of a hack for an unknown programming problem.  Modules can be turned off from configuration.  If this application is deployed to an environment that does not have this issue, then the module can be turned off from configuration.

    Tuesday, May 5, 2020 12:16 PM
  • User2102072086 posted

    yes, it is .asax, ok thank u

    Tuesday, May 5, 2020 1:19 PM