Search code examples
asp.netasp.net-mvchttp-redirectiframe

ASP.NET MVC RedirectToAction with absolute URL


I have written an ASP.NET MVC application that runs inside an IFrame. When one of my controller methods returns RedirectToAction(). I want the top browser URL to redirect to the address, not just my IFrame. How would I go about doing this? Because I am running inside another site, I will need to pass an absolute URL to the browser, i.e. 'http://parentsite.com/myapp/{controller}/{action}'.

I guess it's the equivalent of setting the target attribute of my hyperlinks to '_top' so that the whole site redirects (this will be pretty straightforward by extending the HtmlHelper), but how do I do it for server-side redirects?

So far, my solution is to override OnResultExecuting, extract the URL I intend to redirect to, then instead, redirect to a Frame Breaker View passing the URL I originally intended to Redirect to as a parameter. The Frame Breaker View simply writes out some JavaScript that sets the top browser URL to my original URL. This approach has an extra HTTP request than I would like, but at least doesn't violate any MVC principles (I think!). Thoughts?


Solution

  • Use Redirect() instead of RedirectToAction() and pass in the url.

    Edit:

    I think you'll need some JavaScript to break out of the IFrame on the client side. Redirecting to a url will only affect the current frame.