9

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?

3 Answers 3

9

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.

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

2 Comments

By the way, the latest HTTP/1.1 spec (RFC 7231, § 7.1.2) explicitly says that the Location header contains URI reference and relative URIs are resolved against the effective request URI. There is no need to make the URI reference absolute.
If you actually want to return absolute URI anyway, it goes like this: return Redirect(new Uri(Request.Url, Url.Action(...)).AbsoluteUri);
3

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>

Comments

2

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

return Redirect(Url.YourExtension());

Comments

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.