2

I want to do a simple redirect when a user first loads the website. For that, I just access the RouteData on BeginExecuteCore() method of BaseController and if URL does not have a specific value, then I will redirect to another https route:

  public class BaseController : Controller
  {
        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            // access route data

            if (condition == true)
               Response.Redirect(string.Format("https://{0}/en/en", Request.Url.Authority), true);
            }
        }
  }

Some facts:

  1. I achieve the result I want
  2. I get the Exception "Server cannot append header after HTTP headers have been sent."
  3. I've tried several alternatives: Using other methods to do the redirect like Initialize(RequestContext requestContext) etc
  4. I tried to Use parameter EndResponse=true on Redirect method
  5. I tried to clear the headers before doing the redirect
  6. In all the previous experiments I got the same exception
  7. I'm using MVC 5

My question is: How can I redirect the user to another route in the most efficient way knowing that I need to have access to RouteData and without throwing the annoying exception?

Thank you!

2 Answers 2

1

Response.Redirect(anyUrl) cause 302 status code And Html.AntiForgeryToken() has conflict with 302 status code normally.

You can put the following code in Application_Start:

AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

Security Warning: SuppressXFrameOptionsHeader =true prevents your site from being loaded into an iframe.

read more

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not familiair with the method BeginExecuteCore() as i am still working with mvc 3 and 4.

In mvc 4 i would use an action filter to check something before a page loads.

The reason why you are seeing that error is probably because you are to late redirecting the user as the page is already being loaded.

 public class LogActionFilter : ActionFilterAttribute
   {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
                if (condition == true)
               filterContext.Response.Redirect(string.Format("https://{0}/en/en", Request.Url.Authority), true);
          }     
     }

Take a look at: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

2 Comments

Unfortunately it didn't. I got the same exception. Here's what I did (to be sure I didn't anything that I shouldn't) 1. I adapted your method because there are some change in the way filterContext exposes Request and Response (it worked and I could retrieve the info I needed) 2. Decorated BaseController with with [LogActionFilter]
3. Also tried to decorate HomeController instead of BaseController without success.. This code is called after the previous code I have so I think the probability of trying to redirect the user when the page is being loading is higher

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.