iFrameを使用する必要があると思いますが、すべてが悪いわけではありません。文字列は引き続き表示できます。
コントローラは次のようになります。
public class HomeController : Controller
{
//
// GET: /Home/
//Initial landing page
public ActionResult Index()
{
return View("");
}
//for full postbacks, sets the iframes src on the index view
public ActionResult Page(String url)
{
String myurl = "/Home/Search?url=" + url;
return View("Index", model: myurl);
}
//for the iframes src attribute
public ActionResult Search(String url)
{
//replace pageContent with your html string
String pageContent = "<html><head><title>this is the title</title></head><body>this is the body</body></html>";
return Content(pageContent);
}
}
インデックスはランディングページまたは「検索」ページになり、ページはjavascriptがサポートされていない場合にフォームが投稿される場所になり、検索はiFrameをポイントする場所になります。
アクション:
@model String
@{
ViewBag.Title = "URLer";
}
<script type="text/javascript">
$(document).ready(function () {
$('#searchForm').submit(function (e) {
e.preventDefault();
$('#page').attr('src', '/Home/Search?url=' + $('#url').val());
return false;
});
});
</script>
<div>
@using (Html.BeginForm("Page", "Home", FormMethod.Get, new { id = "searchForm" }))
{
<input id="url" name="url" type="text" />
<input id="Search" type="submit" value="Search" />
}
</div>
<iframe id="page" src="@Model" width="100%" height="500">
</iframe>
これは検索ボックスと送信ボタンを示しています。jQueryを使用して、フォーム送信はiframeのsrc属性を検索アクション、つまり「/ Home / Search」に設定し、URLテキストボックスの値をクエリ文字列の一部として追加します。次に、iFrameをトリガーして、設定されているURL(例として/ Home / Search?url = http://google.com)に移動します。ここで、実際のWebサイトではなく独自の生のhtmlページ/文字列を返します。
フォームはセーフティネットであり、おそらく必要ありませんが、何らかの理由でjavascriptが無効になっている場合、フォームは/ Home / Page?url = http://google.comに投稿され、返されたビューにiframeが設定されます。したがって、検索アクションからURLを取得します。