views:

1453

answers:

3

I have written an Asp .Net MVC applicaton 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 I 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 don't think!). Thoughts?

Thanks

+1  A: 

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.

Dennis Palmer
A: 

I'd recommend extending the HtmlHelper and use it in server side redirects too:

return Redirect(Url.YourExtension());

+1  A: 

Pass your URL back to your view or maybe you could use Url.RouteUrl() in the View itself.

So for example...

public ActionResult Handback()
{
   return View(your_absolute_url);
}

Then your View can use this value to do a redirect. Use Javascript to detect if there is a parent, then set the location else do it in the current window. The example below, instead of using the strongly typed view, uses RouteUrl to a specific controller/action.

Hope this helps.

<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
        if (window.parent != null)
            window.parent.location = '<%= Url.RouteUrl(new { 
                  controller = "MyController", action = "MyAction" }) %>';
        else
            window.location = '<%= Url.RouteUrl(new { 
                  controller = "MyController", action = "MyAction" }) %>';
    </script>
</body>
</html>
David Liddle