Reputation: 41
Server : ASP.net Core Web API
Client : WinForms app using Refit and sending its version with each request (as a header)
How does the server check the client requests version, if it is wrong then reply by calling a controller action VersionError?
This code does not end the request:
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;
}
}
Upvotes: 0
Views: 3442
Reputation: 1
You should not do this in middleware, instead, you should use global Action Filter.
Upvotes: 3
Reputation: 664
Try and replace:
context.Request.Method = "GET";
context.Request.Path = "/api/Error/VersionError";
With:
context.Response.Redirect("/api/Error/VersionError");
return;
Upvotes: 0