個人目測 Request.IsAjaxRequest()這個東西是判斷前台提交過來的header中的 X-Requested-With:XMLHttpRequest來區分是不是ajax請求的。
ASP.NET MVC3 中我們可以在"_ViewStart.cshtml"中指定 Layout為我們定義的"_layout.cshtml"頁,當然,我們還可以在"_ViewStart.cshtml"中根據需要加載不同的布局。_ViewStart.cshtml我定義了公共的視圖,包括頁頭和頁尾。
我想解決的是如果是用jquery 來進行AJAX請求時,不需要加載公共的視圖。
<script type="text/javascript"> $(function () { $('#theLink').click(function () { $.ajax({ url: $(this).attr('href'), type: "GET", success: function (response) { $('#mainContent').html(response); } }); return false; }); }); </script>
辦法如下:
在 ~/Views/ViewStart.cshtml里
@{ Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml"; }
在 controller: public ActionResult Index() { return View(); }