解析接受标头
有人对解析 HTTP Accept 标头有任何建议(或正则表达式)吗?
我正在尝试在 ASP.NET MVC 中进行一些内容类型协商。 似乎没有内置的方式(这很好,因为这里有很多思想流派),但是解析并不完全微不足道,如果有人已经完成了,我宁愿不重新发明轮子很好并且愿意分享。
Does anyone have any suggestions (or a regular expression) for parsing the HTTP Accept header?
I am trying to do some content-type negotiation in ASP.NET MVC. There doesn't seem to be a built in way (which is fine, because there are a lot of schools of thought here), but the parsing is not entirely trivial and I would rather not re-invent the wheel if someone has already done it well and is willing to share.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您看过这篇文章吗? 它提供了一个非常全面的实现来解析 Accept 标头并随后用它做一些有用的事情。
Have you seen this article? It gives a pretty comprehensive implementation for parsing the Accept header and subsequently doing something useful with it.
从 .NET 4.5 开始(我认为微软现在已经让有关框架版本 < 4.5 的信息变得相当模糊),您可以使用 来自
System.Net.Http.Headers
的内置解析器之一:然后您可以执行以下操作:
为您提供所有媒体类型的详细列表,其中首选选项是第一项。 以下是示例中
mediaTypes
结果的 LINQPadDump
:向 这个答案,让我走上正确的道路。
As of .NET 4.5 (I think—Microsoft have made info on framework versions < 4.5 rather obscure these days), you can use one of the the built in parsers from
System.Net.Http.Headers
:Then you can do something like this:
Giving you a nice list of all the media types, where the preferred option is the first item. Here's a LINQPad
Dump
of themediaTypes
result from the example:Hat tip to this answer, for getting me on the right track.
我用 PHP 编写了一个解析器。 它并不复杂,但它会按优先顺序为您提供一系列 mime 类型。
I've written a parser in PHP. It's not complex, but it will give you an array of mime types in order of preference.
ASP.NET Core 解决方案:
我知道这是一个老问题,但对于任何像我一样使用 ASP.NET Core 的人来说,请注意,您可以在
上使用
(或GetTypedHeaders
扩展方法RequestResponse
)对象来获取强类型标头信息,包括Accept
:ASP.NET Core solution:
I know this is an old question but for anyone using ASP.NET Core who might stumble into it like me, note that you can use the
GetTypedHeaders
extension method on theRequest
(orResponse
) object to get strongly-typed header information, including forAccept
:在 php 此处 中找到了另一个实现
Found another implementation in php here
阅读 xml.com 文章后,我决定自己不为 Accept 标头编写函数;)
幸运的是,该文章指出了一个很好的库:https://code.google.com/p/mimeparse/ - 就我而言,我需要它作为 Node.js 模块:https://github.com/kriskowal/mimeparse
After reading the xml.com article I decided to not write a function for the Accept header myself ;)
Fortunately the article points to a good library: https://code.google.com/p/mimeparse/ - in my case I need it as a Node.js module: https://github.com/kriskowal/mimeparse
基于 https://stackoverflow.com/a/49011308/275501 构建.com/users/43140/mark-bell">https://stackoverflow.com/users/43140/mark-bell 上面:
Building on https://stackoverflow.com/a/49011308/275501 from https://stackoverflow.com/users/43140/mark-bell above:
RFC 相当复杂。 如果正则表达式严格遵循这些规则,它会变得好几行长。
如果您已经有了 Accept-header,并忽略引号和参数,您可以执行以下操作来匹配每一对:
*
包含在字符类中,因此不需要任何特殊的正则表达式中的情况。The RFC is quite complex. If the regex where to follow these rules to the letter, it would become several lines long.
If you already have the Accept-header, and ignore the quotes and the parameters, you could do something like this to match each pair:
*
is included in the character class, so it does not need any special case in the regex.