you have a typo in the ajax post. you are passing the FormData, not formData. also the url is wrong. as the controller has the route WeatherForecast, its there twice:
const res = await axios.post("WeatherForecast/WeatherForecast/post", formData);
[Route("WeatherForecast")]
[ApiController]
public class WeatherForecastController : ControllerBase
{
Your action route.
[Route("WeatherForecast/post")]
[HttpPost]
public IActionResult Post([FromForm] FileModel file)
{
I think it is worth mentioning, the "post" route part goes against standard REST patterns The HTTP method (GET, POST, PUT,...) should be used to select the action. Any beginning level Web API tutorial will cover standard REST programming patterns. It
would do a lot of good to go through the tutorials on this sites rather than making up your custom REST patterns.
Member
17 Points
204 Posts
Problem to calling the controller from view
Jan 09, 2021 06:39 AM|SaeedP|LINK
Hello,
Is this URI for Axios correct to call the controller:
Here comes the view and the controller:
If yes, why I can't upload an image?
All-Star
57834 Points
15485 Posts
Re: Problem to calling the controller from view
Jan 09, 2021 05:44 PM|bruce (sqlwork.com)|LINK
you shouldn't double post.
you have a typo in the ajax post. you are passing the FormData, not formData. also the url is wrong. as the controller has the route WeatherForecast, its there twice:
Member
17 Points
204 Posts
Re: Problem to calling the controller from view
Jan 09, 2021 08:54 PM|SaeedP|LINK
I changed the URI. But still can't call the back end.
Even I can't see the log file.
Can it be due to the "MapControllerRoot"?
All-Star
57834 Points
15485 Posts
Re: Problem to calling the controller from view
Jan 09, 2021 10:09 PM|bruce (sqlwork.com)|LINK
I added your sample code to an app created with
dotnet new react
project and with the one change it worked fine (well the file write in the controller failed until I created a wwwroot folder.)
Member
17 Points
204 Posts
Re: Problem to calling the controller from view
Jan 09, 2021 11:05 PM|SaeedP|LINK
Yes, this link works
But why we need to write it twice?
thanks,
All-Star
57834 Points
15485 Posts
Re: Problem to calling the controller from view
Jan 10, 2021 05:01 PM|bruce (sqlwork.com)|LINK
Read documentation on route attributes. You have specified a base route for the controller and additional route paths for the actions
All-Star
52091 Points
23207 Posts
Re: Problem to calling the controller from view
Jan 10, 2021 05:08 PM|mgebhard|LINK
Because you specifically and openly coded the route to require two WeatherForecast parts in the URL! See the official routing documentation to learn the basics. https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
Your controller route.
[Route("WeatherForecast")] [ApiController] public class WeatherForecastController : ControllerBase {
Your action route.
[Route("WeatherForecast/post")] [HttpPost] public IActionResult Post([FromForm] FileModel file) {
I think it is worth mentioning, the "post" route part goes against standard REST patterns The HTTP method (GET, POST, PUT,...) should be used to select the action. Any beginning level Web API tutorial will cover standard REST programming patterns. It would do a lot of good to go through the tutorials on this sites rather than making up your custom REST patterns.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio
Anyway, your action can be changed to the following.
The URL becomes "WeatherForecast" and the HTTP "POST" selects the appropriate method.
Member
17 Points
204 Posts
Re: Problem to calling the controller from view
Jan 10, 2021 07:45 PM|SaeedP|LINK
Thanks a lot for your help and supports.
But if I want to bind a component, for example, an image editor or a grid, to a model, which HTTP request should I use?
Post, Get, Put, Delete?
All-Star
52091 Points
23207 Posts
Re: Problem to calling the controller from view
Jan 10, 2021 08:12 PM|mgebhard|LINK
Um yeah, these fundamentals are openly covered in the tutorials. I think you'll be interested in clicking the link(s) above and reading.
All-Star
57834 Points
15485 Posts
Re: Problem to calling the controller from view
Jan 10, 2021 08:15 PM|bruce (sqlwork.com)|LINK
you really should read a tutorial about coding before starting.
reading any tutorial on REST would cover this.
https://restfulapi.net
REST implements CRUD
Create = post method
Read = get method
Update = put method
Delete = delete method
any beginning tutorial on webapi, would tell you that you name the actions after the method for default mapping
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio
you should also understand the importance of idempotence. this is key to a reliable REST api.
https://restfulapi.net/idempotent-rest-apis/