Asp.Net Core MVC 中的 Request.IsAjaxRequest() 在哪里?

编程入门 行业动态 更新时间:2024-10-21 07:45:39
本文介绍了Asp.Net Core MVC 中的 Request.IsAjaxRequest() 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

要了解有关新的令人兴奋的 Asp.Net-5 框架的更多信息,我正在尝试使用新发布的 Visual Studio 2015 CTP-6 构建一个 Web 应用程序.

To learn more about the new exciting Asp.Net-5 framework, I'm trying to build a web application using the newly released Visual Studio 2015 CTP-6.

大多数事情看起来很有希望,但我似乎找不到 Request.IsAjaxRequest() - 我在旧的 MVC 项目中经常使用的功能.

Most things looks really promising, but I can't seem to find Request.IsAjaxRequest() - a functionality I've been using quite frequently on older MVC projects.

有没有更好的方法来做到这一点——让他们删除这个方法——或者它是否隐藏"在其他地方?

Is there a better way to do this - that made them remove this method - or is it "hidden" somewhere else?

感谢您提供有关在哪里找到它或做什么的建议!

Thanks for any advice on where to find it or what to do instead!

推荐答案

我有点困惑,因为标题提到了 MVC 5.

I got a little confused, because the title mentioned MVC 5.

搜索Ajax 在 MVC6 github repo 中没有给出任何相关结果,但您可以自己添加扩展.MVC5 项目的反编译给出了一段非常简单的代码:

Search for Ajax in the MVC6 github repo doesn't give any relevant results, but you can add the extension yourself. Decompilation from MVC5 project gives pretty straightforward piece of code:

/// <summary> /// Determines whether the specified HTTP request is an AJAX request. /// </summary> /// /// <returns> /// true if the specified HTTP request is an AJAX request; otherwise, false. /// </returns> /// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception> public static bool IsAjaxRequest(this HttpRequestBase request) { if (request == null) throw new ArgumentNullException(nameof(request)); if (request["X-Requested-With"] == "XMLHttpRequest") return true; if (request.Headers != null) return request.Headers["X-Requested-With"] == "XMLHttpRequest"; return false; }

由于 MVC6 Controller 似乎正在使用 Microsoft.AspNet.Http.HttpRequest,您必须检查 request.Headers 集合 通过对 MVC5 版本进行一些调整来获得适当的标头:

Since MVC6 Controller seems to be using Microsoft.AspNet.Http.HttpRequest, you'd have to check request.Headers collection for appropriate header by introducing few adjustments to MVC5 version:

/// <summary> /// Determines whether the specified HTTP request is an AJAX request. /// </summary> /// /// <returns> /// true if the specified HTTP request is an AJAX request; otherwise, false. /// </returns> /// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception> public static bool IsAjaxRequest(this HttpRequest request) { if (request == null) throw new ArgumentNullException(nameof(request)); if (request.Headers != null) return request.Headers["X-Requested-With"] == "XMLHttpRequest"; return false; }

或直接:

var isAjax = request.Headers["X-Requested-With"] == "XMLHttpRequest"

更多推荐

Asp.Net Core MVC 中的 Request.IsAjaxRequest() 在哪里?

本文发布于:2023-11-14 10:05:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Core   Net   Asp   IsAjaxRequest   Request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!