【发布时间】:2020-10-16 17:43:00
【问题描述】:
服务器:ASP.net Core Web API
客户端:WinForms 应用程序使用 Refit 并随每个请求发送其版本(作为标头)
服务器如何检查客户端请求的版本,如果错误则通过调用控制器操作 VersionError 进行回复?
此代码不会结束请求:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthorization();
app.Use(async (context, next) =>
{
foreach (var header in context.Request.Headers)
{
Console.WriteLine($"{context.Request.Path} : {header.Key}={header.Value}");
if (header.Key == "CLIENT-VERSION" && header.Value != "5")
{
context.Request.Method = "GET";
context.Request.Path = "/api/Error/VersionError";
}
}
await next();
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
[ApiController]
[Route("api/[controller]/[action]")]
public class ErrorController : ControllerBase
{
[HttpGet]
public Message<string> VersionError()
{
var reply = new Message<string>(errorMessage: "version error");
return reply;
}
}
【问题讨论】:
标签: c# asp.net-web-api asp.net-core-middleware