dotnet / dotnet/aspnetcore

HttpPost 405 error in some controllers

Open
#58,946 2 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### 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:
```






















```

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> CreateAsyc([FromBody] CreateTableIndex request)
=> await Mediator.Send(request);

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

[HttpPut("save")]
public async Task> UpdateAsyc([FromBody] UpdateTableIndex request)
=> await Mediator.Send(request);
}
```
angular service:
```
post(url: string, param: TPost): Observable {
return this
.http
.post(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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.