0

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;
    }
}

2 Answers 2

3

You should not do this in middleware, instead, you should use global Action Filter.

Sign up to request clarification or add additional context in comments.

1 Comment

Not an anwser..
0

Try and replace:

context.Request.Method = "GET";
context.Request.Path = "/api/Error/VersionError";

With:

context.Response.Redirect("/api/Error/VersionError");
return;

1 Comment

Refit.ApiException: Response status code does not indicate success: 302 (Found).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.