什么是检测在ASP.NET一个JSON请求的最佳途径 [英] What's the best way to detect a JSON request on ASP.NET

查看:115
本文介绍了什么是检测在ASP.NET一个JSON请求的最佳途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数Ajax框架似乎不论是在头或查询字符串与X-要求,以规范。

Most ajax frameworks seem to standardize with "X-Request-With" on either a header or the query string.

和ASP.NET MVC中,你可以使用扩展方法

And in ASP.NET MVC you can use the extension method

Request.IsAjaxRequest()

由于一个AJAX客户端可以请求多种不同的内容类型,而不仅仅是应用/ JSON比如:应用程序/ XML。

Because an ajax client can request several different content types, not just "application/json" ex: "application/xml".

我用下面的code段/扩展方法,但我希望能看到别人在做什么,或者有什么我错过了,或者更好的办法。

I'm using the following code snippet/extension method, but I would love to see what others are doing or if there is something I missed, or a better way.

public static bool IsJsonRequest(this HttpRequestBase request)
{
    return request.Headers["Accept"].Split(',') 
       .Any(t => t.Equals("application/json", StringComparison.OrdinalIgnoreCase));
}

在此先感谢。

Thanks in advance.

推荐答案

为什么不能你只是传递一个布尔变量说IsJsonRequest从那里你正在请求的客户端?

Why cant you just pass a bool variable say IsJsonRequest from the client where you are making request?

然后使操作方法进行检查。

Then make a check in action method.

您可以使用的接受头为这个请求。这表示客户端想要的服务器发送给它的内容类型。

You could use the request's accept header for this. This indicates what type of content the client wants the server to send to it.

这篇关于什么是检测在ASP.NET一个JSON请求的最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆