最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Executing endpoint '405 HTTP Method Not Supported' - Stack Overflow

programmeradmin2浏览0评论

I'm developing an Angular application that communicates with an ASP.NET Core backend. When attempting to call the IsRequestValid endpoint from Angular, I receive a 405 HTTP Method Not Supported error. However, invoking the same endpoint via Swagger UI works as expected.​

Angular service method:

@Injectable({ providedIn: 'root' })
export class UserAppService {
private apiUrl = '/weatherforecast';

constructor(private http: HttpClient) { }

isRequestValid(input: xxxUserDto): Observable\<xxxUserDto\> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post\<xxxUserDto\>(`${this.apiUrl}/IsRequestValid`, input, { headers });
}
}

Angular component method:

this.userService.isRequestValid(this.UI_User)
.pipe(
catchError((error) =\> {
this.toaster.error('Fehler beim Validieren des Tokens.');
return throwError(() =\> error);
})
)
.subscribe((response) =\> {
this.UI_User = response;
this.setCountDown();
});

Backend controller:

\[ApiController\]
\[Route("\[controller\]")\]
public class WeatherForecastController : ControllerBase
{
// Constructor and other methods

    [HttpPost("IsRequestValid")]
    public async Task<ActionResult<xxxUserDto>> IsRequestValid([FromBody] xxxUserDto input)
    {
        // Method implementation
    }

}

Serilog output:

2025-03-31 15:54:10.964 +02:00 \[INF\] Request starting HTTP/1.1 POST https://localhost:52288/weatherforecast/IsRequestValid - application/json 123
2025-03-31 15:54:11.060 +02:00 \[INF\] Executing endpoint '405 HTTP Method Not Supported'
2025-03-31 15:54:11.064 +02:00 \[INF\] Executed endpoint '405 HTTP Method Not Supported'

Vite proxy error:

15:57:02 \[vite\] http proxy error: /weatherforecast/IsRequestValid
AggregateError \[ECONNREFUSED\]:
at internalConnectMultiple (node:net:1139:18)
at afterConnectMultiple (node:net:1712:7)

What changes are necessary to successfully call the IsRequestValid endpoint from my Angular application without encountering the 405 error?

I'm developing an Angular application that communicates with an ASP.NET Core backend. When attempting to call the IsRequestValid endpoint from Angular, I receive a 405 HTTP Method Not Supported error. However, invoking the same endpoint via Swagger UI works as expected.​

Angular service method:

@Injectable({ providedIn: 'root' })
export class UserAppService {
private apiUrl = '/weatherforecast';

constructor(private http: HttpClient) { }

isRequestValid(input: xxxUserDto): Observable\<xxxUserDto\> {
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post\<xxxUserDto\>(`${this.apiUrl}/IsRequestValid`, input, { headers });
}
}

Angular component method:

this.userService.isRequestValid(this.UI_User)
.pipe(
catchError((error) =\> {
this.toaster.error('Fehler beim Validieren des Tokens.');
return throwError(() =\> error);
})
)
.subscribe((response) =\> {
this.UI_User = response;
this.setCountDown();
});

Backend controller:

\[ApiController\]
\[Route("\[controller\]")\]
public class WeatherForecastController : ControllerBase
{
// Constructor and other methods

    [HttpPost("IsRequestValid")]
    public async Task<ActionResult<xxxUserDto>> IsRequestValid([FromBody] xxxUserDto input)
    {
        // Method implementation
    }

}

Serilog output:

2025-03-31 15:54:10.964 +02:00 \[INF\] Request starting HTTP/1.1 POST https://localhost:52288/weatherforecast/IsRequestValid - application/json 123
2025-03-31 15:54:11.060 +02:00 \[INF\] Executing endpoint '405 HTTP Method Not Supported'
2025-03-31 15:54:11.064 +02:00 \[INF\] Executed endpoint '405 HTTP Method Not Supported'

Vite proxy error:

15:57:02 \[vite\] http proxy error: /weatherforecast/IsRequestValid
AggregateError \[ECONNREFUSED\]:
at internalConnectMultiple (node:net:1139:18)
at afterConnectMultiple (node:net:1712:7)

What changes are necessary to successfully call the IsRequestValid endpoint from my Angular application without encountering the 405 error?

Share Improve this question edited Mar 31 at 16:12 Michał Turczyn 37.8k17 gold badges54 silver badges82 bronze badges asked Mar 31 at 14:12 TesterTester 1 New contributor Tester is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • In your angular app, is the "input" a valid json? – SoftwareDveloper Commented Mar 31 at 19:33
Add a comment  | 

2 Answers 2

Reset to default 0

This is because Angular app is running on different host (this is different application, and so it will run on different host, locally it will run on different port).

And in your Angular app you have used this as URL for POST method:

private apiUrl = '/weatherforecast';

Then ${this.apiUrl}/IsRequestValid would return /weatherforecast/IsRequestValid, which would make a HTTP call to this endpoint, but base URL will be pointing to frontend application (so localhost with port different than your ASP.NET applicaiton). And your Angular app does not expose this endpoint, thus the problem.

In order to correct it, define apiUrl as full URL, together with base URL for backend ASP.NET application:

private apiUrl = 'http://localhost:[ASP.NET port]/weatherforecast';

Pass correct Url. private apiUrl = 'http://localhost:5000/WeatherForecast';

发布评论

评论列表(0)

  1. 暂无评论