其实我就是这么用的。你有更好的方法吗?
private bool AcceptJson(HttpRequest request)
{
const string JsonType = "application/json";
if (request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith(JsonType))
{
return true;
}
if (request.AcceptTypes.Select(t => t.ToLower(CultureInfo.InvariantCulture) == JsonType).Count() > 0)
{
return true;
}
return false;
}
最佳答案
这种方法可能会导致误报(它不考虑 q 值或 application/json 是其子字符串的内容类型)。
您可以在 this article about XHTML 中找到一个不错的 Accept header 解析器.您必须将算法移植到您选择的语言,并使其适应您正在使用的内容类型。
关于c# - 有没有更好的方法来验证请求是否接受 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/175236/