0

I'm trying to create API working by minimalApi service who can be accessible from public and can send GET/POST/PUT request to my Web Service with Controllers.

But every time when I Send GET to my controller, I receive status code 200 (OK) even when i sure that action from controller 100% always must return bad request, I'm assuming that no request was actually sent. When I'm trying to send POST or PUT, I get status code 405 (Method not allowed) So I don't have idea how to send any request to another web service.

My Simple minimalAPi action :

  app.MapGet("/SendTestRequest", async ( HttpClient httpClient ) =>
    {
        HttpRequestMessage httpRequestMessage = new() 
        { 
            RequestUri = new Uri($"https://localhost:44313/TestNotification/SendTestRequest"), 
            Method = HttpMethod.Get
        };
        HttpResponseMessage result = await httpClient.SendAsync(httpRequestMessage);
        result.EnsureSuccessStatusCode();
    });

And my Controller from other side :

[EnableCors("cors")]
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> SendTestRequest()
{
    return BadRequest();  
}

After research, I assumed that problem is because of CORS, And tried to configure CORS on both sides. And it looks good for me, but it still doesn't work.

CORS Config on MinimalApi Web application Side :

Program.cs...
builder.Services.AddHttpClient();
builder.Services.AddCors(options =>
{
    options.AddPolicy(name: "cors",

        policy =>
        {
            policy.AllowAnyOrigin();
            policy.AllowAnyMethod();
        });
});

app.UseCors("cors");

CORS config on MVC Web application :

ConfigureServices section ...
        services.AddCors(options =>
        {
            options.AddPolicy(name: "cors",
                policy =>
                {
                    policy.AllowAnyOrigin();
                    policy.AllowAnyMethod();
                    policy.AllowAnyHeader().WithExposedHeaders("*");
                });
        });

 Configure section ... 
            app.UseCors("cors");
Igor Markiv
  • 147
  • 12
  • CORS here is not relevant in no shape or form - you are not setting the origin header on your request (and it would not change bad request to ok). – Guru Stron Jan 12 '23 at 13:42
  • Try invoking `https://localhost:44313/TestNotification/SendTestRequest` from browser - does it return bad request as expected? – Guru Stron Jan 12 '23 at 13:44
  • Its return status Code 304 – Igor Markiv Jan 12 '23 at 13:51
  • So it seems something fishy is going with the API either way. – Guru Stron Jan 12 '23 at 13:58
  • Then I need to clarify the structure of the solution. I have WASM client + server side MVC (with that target controller) and they share the same address , And I have small web application on MinimalApi, i hope this will help you and better clarify the situation – Igor Markiv Jan 12 '23 at 14:07

1 Answers1

0

For some Reason, I can't directly call the server side controller in blazor, I bypassed this problem manually configured Get Action in Endpoints

 Configure section ....
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapGet("/test", Test);
                endpoints.MapFallbackToFile("index.html");
                endpoints.MapHub<Hubs.ServerHub>(Hubs.ServerHub.HubUrl);
            });
    
Somewhere in prodject...
            public IResult Test()
            {
                var t = TypedResults.Ok(new Random().Next());
                return t;
            }
Igor Markiv
  • 147
  • 12