HttpPost forms in ASP.NET

Hello, i'm having a hard time understanding how clicking the submit button on this form triggers the CreateCategory(Category entry) IActionResult method. I understand that [HttpPost] determines which overload of the CreateCategory() method gets activated.. But why CreateCategory()?? There's nothing in the form linking to this. If it's automatic then how would it make distinctions between multiple [HttpPost] methods?
No description
No description
21 Replies
tera
tera8mo ago
its possible specify [HttpPost("custom/path/here")]. your CreateCategory method doesnt explicitly provide a custom path. this means path will be path of the controller. so with that in mind, when your view (GET CreateCategory) gets loaded, a submit button on that view with no explicit path set will point to same path as it's current page. so you click the button and it just sends a POST to current url.
tera
tera8mo ago
Routing to controller actions in ASP.NET Core
Learn how ASP.NET Core MVC uses Routing Middleware to match URLs of incoming requests and map them to actions.
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
i still dont understand how i can get my form to point towards a specific POST method
No description
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
here i am specifying the path of my HttpPost method, but not the submit button on a form that points to it
No description
tera
tera8mo ago
i didnt say you need to change anything 😄
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
i know i mean, it works, but what if i have multiple HttpPost methods then how will the program know which one to activate at the press of "submit"? or is that something asp.net devs don't concern with.. im not sure
tera
tera8mo ago
you submit to a different path.. idk where is the relevant docs, but explore around that sub page you would specify probably in a html tag which path your submit button sends a POST to as an attribute of form probably if you know how it's done in plain HTML form then it probably makes sense
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
ive tried action="Category/CreateCategory" but fails
tera
tera8mo ago
where actually take a look here
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
in the form
No description
tera
tera8mo ago
and the page right below it
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
okay ill check it out Thank you mister cat
tera
tera8mo ago
😄
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
trying dis
No description
No description
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
boo womp oh wait no im silly yeah im silly, i shouldnt write "CategoryController" but "Category" it now works perfectly
tera
tera8mo ago
nice
☠  Pointman ☠
☠ Pointman ☠OP8mo ago
>looks at documentation >tag works fine "guhh? How could this be?!" pointman misadventures
tera
tera8mo ago
haha
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX8mo ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?