Redirect to an action from Application_BeginRequest in global.asax
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.
Null Pointer
Develops Windows Phone and Windows 8 apps SOreadytohelp
Updated on November 13, 2020Comments
-
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
-
Baz1nga over 12 yearsPossible duplicate of stackoverflow.com/questions/580728/…
-
-
Fourat over 8 yearsThis will create a loop
-
O'Rooney about 8 years@Null Pointer, please could you accept one of the other answers that works?
-
Richard Barker over 7 yearsThis will cause another request; which will cause another request. Which causes another request. Which creates another. Until it errors out completely.
-
Richard Barker over 7 yearsThat 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 over 7 yearsThis causes a redirect loop to occur if used in the Application_beginRequest function.
-
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 over 7 yearsMy apologies. The question showed BeginRequest. May I suggest you update your answer to show that and explain why you did it?
-
Richard Barker over 7 yearsThat'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 over 7 yearsI 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!