使用 javascript 从 MVC Action 通过 iFrame 重定向到父级

Redirect to parent via iFrame from MVC Action using javascript

本文关键字:重定向 iFrame 通过 javascript MVC Action 使用      更新时间:2023-09-26

我在Webforms站点的iFrame中有一个MVC表单。表单通过以下代码提交,但我希望它重定向到 WebForms 站点上的父页面。它不起作用,因为我无法让重定向结果以目标父级。从我过去学到的情况来看,这做不到吗?

[HttpPost]
public ActionResult Index(string FindText, string FindTown)
{
    return new RedirectResult("http://www.thesite.com/SearchResults.aspx?SearchText=" + SearchText + "&Town=" + Town);
}

有没有办法通过 Javascript 从操作内部定位父级,以实现我想要的结果?

例如,使用,

window.parent.location.href

如果这可能,我将如何写?

你可以尝试返回 JavaScriptResult:

return new JavaScriptResult 
{
    Script = string.Format("window.parent.location.href = '{0}'", url)
};

或内容结果:

return ContentResult
{
    Content = string.Format("<script type="text/javascript">window.parent.location.href = '{0}';</script>", url),
    ContentType = "text/html"
};