[英]Manual content negotiation in ASP.NET Core
Is it possible to access the methods that ASP.NET Core uses to negotiate content?是否可以访问 ASP.NET Core 用于协商内容的方法?
For example, I would expect a method that does:例如,我希望有一种方法可以:
var acceptHeader = "text/*, text/html, text/html;level=1, */*";
var candidates = new[] { "image/png", "text/plain" };
var preferred = Negotiate(candidates, acceptHeader) // returns text/plain.
I found a library that does it.我找到了一个可以做到这一点的图书馆。 As ASP.NET Core is doing content negotiation behind the scenes I would prefer to use .NET Core methods to avoid adding a new library to the project.
由于 ASP.NET Core 在幕后进行内容协商,我更愿意使用 .NET Core 方法来避免向项目添加新库。
You can build your own middleware component to achieve this goal.您可以构建自己的中间件组件来实现此目标。
public async Task Invoke(HttpContext context)
{
Negotiate(context);
await _next(context);
}
And don't forget to register your middleware并且不要忘记注册您的中间件
services.AddScoped<IMiddleware, SomeMiddleware>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.