.NET WebForms app - redirect

Jaak Ivask 1 Reputation point
2024-02-29T11:37:05.44+00:00

Hello,

I have an old WebForms e-commerce app.

  • We do not use the front-end part anymore, but in reality, the front-end part is available using the address office.company.com/.* We want to redirect this part to the new front-end URL www.company.com.
  • From the old app, we are using the office.company.com/administrator/ and  office.company.com/someASPX.aspx  file. These url's should not be redirected.

How to write redirects according to these conditions? From my searches, there are several possibilities:

  • app web.config file
  • IIS server configuration
  • ....

 

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,248 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2024-02-29T13:12:48.9466667+00:00

    Use the Application_BeginRequest handler in the Global.asax file.

    void Application_BeginRequest(Object source, EventArgs e)
    {
        Uri url =  HttpContext.Current.Request.Url;
        if(url.AbsolutePath != "The page I'm looking for " | url.AbsolutePath != "The other page I'm looking for ")
        {
            Response.Redirect("to the URL I want to redirect to");
        }
    }
    

    I used url.AbsolutePath but there are other properties that might be better for your situation. See the docs. https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=net-8.0

    0 comments No comments

  2. Lan Huang-MSFT 25,471 Reputation points Microsoft Vendor
    2024-03-01T05:50:32.0166667+00:00

    Hi @Jaak Ivask,

    One general idea is to capture on BeginRequest the url in the Global.asax file, and redirect if necessary, here is an example:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
    
        if (Request.RawUrl.StartsWith("http://b.com"))
        {
            app.Response.Redirect(Request.RawUrl.Replace("http://b.com", "http://a.com"), true);            
            return;
        }       
    }
    

    If you want to choose IIS rewriting, you can refer to the following solutions.

    https://stackoverflow.com/a/9332671

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments