zoukankan      html  css  js  c++  java
  • ASP.NET MVC中通过Request.IsAjaxRequest()来判断是否要加载公共视图

    个人目测 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();
    }          
  • 相关阅读:
    Java 方法重载 (Overload)
    Java 向数组中添加一个元素
    Java 三目运算符
    代理池的维护
    代理设置
    验证码识别
    使用Selenium爬取淘宝商品
    Splash API 调用
    Android ListView中Item点击事件失效解决方案
    mapreduce框架详解
  • 原文地址:https://www.cnblogs.com/nele/p/4974840.html
Copyright © 2011-2022 走看看