How to Make a PUT Request from ASP.NET Core MVC to Web API
Автор: vlogize
Загружено: 2025-05-27
Просмотров: 1
Описание:
Learn how to resolve the 'Method Not Allowed' error when making a `PUT` request in ASP.NET Core MVC to a Web API. Follow this guide for step-by-step instructions and best practices.
---
This video is based on the question https://stackoverflow.com/q/69661320/ asked by the user 'Psychonaut007' ( https://stackoverflow.com/u/16972919/ ) and on the answer https://stackoverflow.com/a/69661806/ provided by the user 'Serge' ( https://stackoverflow.com/u/11392290/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to make a PUT request from ASP.NET core mvc to Web API in asp.net core?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Introduction
Working with Web APIs in ASP.NET Core MVC can hit a few bumps along the road, especially when it comes to sending data using HTTP methods like PUT. If you've encountered the error StatusCode: 405, ReasonPhrase: 'Method Not Allowed', you're not alone. This guide will explore the common pitfalls in sending a PUT request and provide you with a clear solution to get your application back on track.
The Problem
You need to update model data via an API call but run into a 405 Method Not Allowed error. After successfully testing your API independently, you face difficulties making it work within your ASP.NET Core MVC application. The root of this issue lies in how you're defining the route and handling the HTTP request.
Understanding the Code
Let's break down the current code structure to identify the problem areas.
Customer Model Class
Here's a simple model for a Customer:
[[See Video to Reveal this Text or Code Snippet]]
PUT Method in API
In your API controller, you have the following method:
[[See Video to Reveal this Text or Code Snippet]]
The route here is problematic since it expects a route parameter {customer} but you’re sending the object in the body of the request instead.
Calling the API
Here's how you attempt to call the API:
[[See Video to Reveal this Text or Code Snippet]]
EditCustomer Method
Your implementation for editing customer data looks like this:
[[See Video to Reveal this Text or Code Snippet]]
The Solution
To fix the issue and properly handle the PUT request, follow these steps:
Update the API Route
Remove the {customer} from the route definition, since you're sending the customer object in the request body:
[[See Video to Reveal this Text or Code Snippet]]
Update the API Call
Change your PUT request to:
[[See Video to Reveal this Text or Code Snippet]]
Alternatively, for better clarity, you could rename the route to something more descriptive:
[[See Video to Reveal this Text or Code Snippet]]
And adjust the API call accordingly:
[[See Video to Reveal this Text or Code Snippet]]
Use StringContent for the Request
To increase reliability when sending the customer object, consider using StringContent as shown below. This helps avoid issues that may arise with PutAsJsonAsync:
[[See Video to Reveal this Text or Code Snippet]]
Final Recommendations
Consider Using POST Instead: If applicable, you might want to use a POST request for updating resources instead of PUT.
Add FromBody Attribute: To specify the incoming data format explicitly, you can modify the action method as follows:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
In conclusion, the 405 Method Not Allowed error can often be resolved by ensuring the API route correctly reflects how data is sent and received. By following the steps outlined above, you can ensure your ASP.NET Core MVC application communicates smoothly with your Web API. Happy coding!
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: