Redirect to an action from Application_BeginRequest in global.asax

59,281

Solution 1

Use the below code for redirection

   Response.RedirectToRoute("Default");

"Default" is route name. If you want to redirect to any action,just create a route and use that route name .

Solution 2

All above will not work you will be in the loop of executing the method Application_BeginRequest.

You need to use

HttpContext.Current.RewritePath("Home/About");

Solution 3

Besides the ways mentioned already. Another way is using URLHelper which I used in a scenario once error happend and User should be redirected to the Login page :

public void Application_PostAuthenticateRequest(object sender, EventArgs e){
    try{
         if(!Request.IsAuthenticated){
            throw  new InvalidCredentialException("The user is not authenticated.");
        }
    } catch(InvalidCredentialException e){
        var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
        Response.Redirect(urlHelper.Action("Login", "Account"));
    }
}

Solution 4

Try this:

HttpContext.Current.Response.Redirect("...");

Solution 5

I do it like this:

        HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);

        RouteData routeData = new RouteData();
        routeData.Values.Add("controller", "Home");
        routeData.Values.Add("action", "FirstVisit");

        IController controller = new HomeController();

        RequestContext requestContext = new RequestContext(contextWrapper, routeData);

        controller.Execute(requestContext);
        Response.End();

this way you wrap the incoming request context and redirect it to somewhere else without redirecting the client. So the redirect won't trigger another BeginRequest in the global.asax.

Share:
59,281
Null Pointer
Author by

Null Pointer

Develops Windows Phone and Windows 8 apps SOreadytohelp

Updated on November 13, 2020

Comments

  • Null Pointer
    Null Pointer over 3 years

    In my web application I am validating the url from glabal.asax . I want to validate the url and need to redirect to an action if needed. I am using Application_BeginRequest to catch the request event.

      protected void Application_BeginRequest(object sender, EventArgs e)
        {
            // If the product is not registered then
            // redirect the user to product registraion page.
            if (Application[ApplicationVarInfo.ProductNotRegistered] != null)
            {
                 //HOW TO REDIRECT TO ACTION (action=register,controller=product)
             }
         }
    

    Or is there any other way to validate each url while getting requests in mvc and redirect to an action if needed

  • Fourat
    Fourat over 8 years
    This will create a loop
  • O'Rooney
    O'Rooney about 8 years
    @Null Pointer, please could you accept one of the other answers that works?
  • Richard Barker
    Richard Barker over 7 years
    This will cause another request; which will cause another request. Which causes another request. Which creates another. Until it errors out completely.
  • Richard Barker
    Richard Barker over 7 years
    That causes another request. Which in turns makes another,. Which makes another. And so on when you're in the application_begin request function.
  • Richard Barker
    Richard Barker over 7 years
    This causes a redirect loop to occur if used in the Application_beginRequest function.
  • Nelly Sattari
    Nelly Sattari over 7 years
    @RichardBarker This code is not located in application_begin request!!!! I put it in Application_PostAuthenticateRequest an in the catch block in cas error happend
  • Richard Barker
    Richard Barker over 7 years
    My apologies. The question showed BeginRequest. May I suggest you update your answer to show that and explain why you did it?
  • Richard Barker
    Richard Barker over 7 years
    That's a bit better, but I created a short complete code example from your update. That will help future stackers by making it easier to understand and also utilize. I believe this to be the best solution for the issue described in the OP - it will avoid the loop issue but first it needs one more check for the current request being for the login action.
  • Robin French
    Robin French over 7 years
    I like this as it generates the MVC route from the helper and will inherently handle a change to the routing. I'm never comfortable hard coding MVC routes as strings!