dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.5k stars 10.04k forks source link

HttpPost 405 error in some controllers #58946

Open whebz opened 3 days ago

whebz commented 3 days ago

Is there an existing issue for this?

Describe the bug

I am encountering a strange problem.

In our application I have a single controller where PUT, DELETE, POST getting 405 error while the rest of controllers are working properly.

here is how my Web.Config is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
        <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            <remove name="WebDAV"/>
        </handlers>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="WebDAVModule"/>
        </modules>
        <aspNetCore processPath="dotnet" 
                    arguments=".\WebUI.dll" 
                    stdoutLogEnabled="false" 
                    stdoutLogFile=".\logs\stdout" 
                    hostingModel="inprocess" />
        <httpProtocol>
            <customHeaders>
                <clear />
                <add name="Access-Control-Expose-Headers " value="*"/>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Methods" value="*" />
                <add name="Access-Control-Allow-Headers" value="*" />
                <remove name="X-Powered-By" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: B10BA5B4-B02C-4528-9272-8478A9F7B038-->

Here is my startup.cs


// configure services
        #region CORS

        services.AddCors(options =>
        {
            options.AddPolicy(
                AllowedOrigins,
                builder =>
                {
                    builder.AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                });
        });

        #endregion

// under configure
    app.UseCors(AllowedOrigins);

From controller I am getting this Response header:

Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: *
Allow: GET, HEAD
Cache-Control: no-cache,no-store
Date: Thu, 07 Nov 2024 09:44:21 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/8.5
Transfer-Encoding: chunked

Controller:

using Application.Common.DTO;
using Application.TableObjects;
using Application.TableObjects.Commands;
using Application.TableObjects.Queries;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using RI.QueryExecutor;

namespace WebUI.Controllers;

public class ListTableController : ApiControllerBase
{
    public ListTableController(ISender sender, IHttpContextAccessor httpContextAccessor)
        : base(sender, httpContextAccessor) { }

    [HttpPost("save")]
    public async Task<IResult<TableIndexDto>> CreateAsyc([FromBody] CreateTableIndex request)
        => await Mediator.Send(request);

    [HttpDelete("save")]
    public async Task<IResult<TableIndexDto>> UpdateAsyc([FromQuery] DeleteTableIndex request)
        => await Mediator.Send(request);

    [HttpPut("save")]
    public async Task<IResult<TableIndexDto>> UpdateAsyc([FromBody] UpdateTableIndex request)
        => await Mediator.Send(request);
}

angular service:

post<TPost, TOut>(url: string, param: TPost): Observable<TOut> {
    return this
      .http
      .post<TOut>(url, param, this.buildHttpOptions())
      .pipe(catchError(_ => this.handleError(_)));
  }

  private buildHttpOptions(): { headers: HttpHeaders; } {
    return {
      headers: this.buildBaseHeaders()
    };
  }

  private buildBaseHeaders(): HttpHeaders {
    const lang: string = 'it';
    const user:string = STRING_EMPTY;
    const datow: string = STRING_EMPTY;
    return new HttpHeaders(this.GetHttpHeaderObject(user, lang, datow));
  }

  private GetHttpHeaderObject(user: string, lang: string, datow: string) : any {
    return {
      'Cache-Control': 'no-cache, no-store, must-revalidate, post-check=0, pre-check=0',
      'Pragma': 'no-cache',
      'Content-Type': 'application/json',
      'Accept-Language': `${lang}-${lang.toUpperCase()}`
    }
  }

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8

Anything else?

No response

mikekistler commented 1 day ago

Can you try issuing the request with curl to see if it works that way? I suspect that your client app is not using the correct path but I don't know angular so this is just a guess. But if we can confirm that the endpoints work from curl that would point in that direction.