简体   繁体   English

ASP.NET Core 中的手动内容协商

[英]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.

相关问题 ASP.NET Web API中的内容协商 - Content Negotiation in ASP.NET Web API 我可以使用内容协商将视图返回到ASP.NET核心中的浏览器和JSON到API调用吗? - Can I use Content Negotiation to return a View to browers and JSON to API calls in ASP.NET Core? ASP.NET 核心 IHostedService 手动启动/停止/暂停(?) - ASP.NET Core IHostedService manual start/stop/pause(?) 在 ASP.NET Core 依赖注入中手动注册控制器 - Manual controller registration in ASP.NET Core dependency injection 使用ASP.Net Core的NancyFX中的静态内容 - Static Content in NancyFX with ASP.Net Core 在 ASP.Net Core 中请求内容解压 - Request content decompression in ASP.Net Core 使用ASP.NET Core 2和Castle.Core的手动拦截无法解析一般服务 - Can not resolve a generic service using manual interception with ASP.NET Core 2 and Castle.Core 在 ASP.NET Core 中,自定义验证属性相对于手动验证的优势? - Benefits of Custom Validation attributes over manual validation in ASP.NET Core? 在本地 IIS 上发布 ASP.Net Core App - 找不到内容 - Publish ASP.Net Core App on local IIS - Content not found 用于抽象模型的Asp.Net Core 2.0(MVC)Content Binder - Asp.Net Core 2.0 (MVC) Content Binder for an abstract model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM