Home > Net >  Asp.net core 2.1 webapi cannot request HttpPost method
Asp.net core 2.1 webapi cannot request HttpPost method

Time:09-21

 
The namespace WebApplication3. Controllers
{
[the Route (" API//controller ")]
[ApiController]
Public class ValuesController: ControllerBase
{
//GET the API/values
[HttpGet]
Public ActionResult The Get ()
{
Return a new string [] {" value1 ", "value2"};
}


[HttpPost (" test ")]
Public string test (string username)
{
Return "username=" + username;
}


Wrote a simple httpPost method test, compiled after the operation can't request, prompt can not find this address, but the httpGet method can request, could you tell me what reason be?

CodePudding user response:

Request to address it and see

CodePudding user response:

https://localhost:44322/api/values/test

CodePudding user response:

HttpPost (" test ") is not the test action alias?
[the Route (" API//controller ")] is the corresponding API/values, so should not be normal API/values/test?

CodePudding user response:

I at Startup. The app in the cs. UseMvc change to app. UseMvcWithDefaultRoute not ();
Request the address is: https://localhost:44322/values/test

CodePudding user response:

Username how to preach the cords, and hierarchy for the form, routing, url address

CodePudding user response:

UseMvcWithDefaultRoute
- "this is in the use of MVC is to set the default routing path, suggest first introduce about MVC and webapi set way,

CodePudding user response:

I now is when the MVC controller to use, has removed the tags:
[the Route (" API//controller ")]
[ApiController]
Just use the default routing model, write a simple HttpPost method, with no arguments, tip 404 couldn't find it

CodePudding user response:

refer to 6th floor exception92 response:
UseMvcWithDefaultRoute
- "this is in the use of MVC is to set the default routing path, Suggestions about first know the set mode, the MVC and webapi


Excuse me how should set webapi routing?

CodePudding user response:

Like the .
I use the postman test can call the post method, had been using the browser to debug;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Before the old asp.net MVC can be in the browser calls the post operation of ah, why to asp.net core not???????

CodePudding user response:

[the Route (" API//controller ")]
You now, this is called visit routing control rule is "API/controller name", the only access to the controller, can not access the methods in the controller,
Change it to:
[the Route (" API/[controller]/[action] ")]
Ok, use the
https://localhost:44322/api/values/test
Note: the API must add, theoretically, the party in writing what is what,
Such as: [the Route (" PPP/[controller]/[action] ")], visit: https://localhost:44322/ppp/values/test
  • Related