Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpPost 405 error in some controllers #58946

Open
1 task done
whebz opened this issue Nov 14, 2024 · 2 comments
Open
1 task done

HttpPost 405 error in some controllers #58946

whebz opened this issue Nov 14, 2024 · 2 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@whebz
Copy link

whebz commented Nov 14, 2024

Is there an existing issue for this?

  • 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:

<?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

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Nov 14, 2024
@mikekistler
Copy link
Contributor

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.

@whebz
Copy link
Author

whebz commented Nov 19, 2024

Hi,
here is an example :

curl -v 'POST' \
  'http://10.1.1.112/control-tower/api/reports/meat-monitor' \
  -H 'accept: text/plain' \
  -H 'Content-Type: application/json' \
  -d '{
  "warehouseCode": "066"
}'
* Could not resolve host: POST
* Closing connection
curl: (6) Could not resolve host: POST
*   Trying 10.1.1.112:80...
* Connected to 10.1.1.112 (10.1.1.112) port 80
> POST /control-tower/api/reports/meat-monitor HTTP/1.1
> Host: 10.1.1.112
> User-Agent: curl/8.6.0
> accept: text/plain
> Content-Type: application/json
> Content-Length: 28
>
< HTTP/1.1 405 Method Not Allowed
< Cache-Control: no-cache,no-store
< Pragma: no-cache
< Transfer-Encoding: chunked
< Allow: GET, HEAD
< Expires: -1
< Server: Microsoft-IIS/10.0
< X-Powered-By: ASP.NET
< Date: Tue, 19 Nov 2024 15:09:40 GMT
<
* Leftovers after chunking: 5 bytes
* Connection #1 to host 10.1.1.112 left intact

The endpoint is visible on swagger:
Image

same api on other server works fine

curl -v 'POST' \
  'http://10.1.1.141/control-tower/api/reports/meat-monitor' \
  -H 'accept: text/plain' \
  -H 'Content-Type: application/json' \
  -d '{
  "warehouseCode": "066"
}'
* Could not resolve host: POST
* Closing connection
curl: (6) Could not resolve host: POST
*   Trying 10.1.1.141:80...
* Connected to 10.1.1.141 (10.1.1.141) port 80
> POST /control-tower/api/reports/meat-monitor HTTP/1.1
> Host: 10.1.1.141
> User-Agent: curl/8.6.0
> accept: text/plain
> Content-Type: application/json
> Content-Length: 28
>
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=utf-8
< Server: Microsoft-IIS/10.0
< X-Powered-By: ASP.NET
< Date: Tue, 19 Nov 2024 15:23:38 GMT
<
{
  "messages": [],
  "succeeded": true,
  "data": [],
  "exception": null,
  "code": 0
* Leftovers after chunking: 11 bytes
* Connection #1 to host 10.1.1.141 left intact
}%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

2 participants