C
C#•3w ago
Pointman

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
tera•3w 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
tera•3w 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•3w ago
i still dont understand how i can get my form to point towards a specific POST method
No description
Pointman
Pointman•3w 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
tera•3w ago
i didnt say you need to change anything 😄
Pointman
Pointman•3w 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
tera•3w 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•3w ago
ive tried action="Category/CreateCategory" but fails
tera
tera•3w ago
where actually take a look here
Pointman
Pointman•3w ago
in the form
No description
tera
tera•3w ago
and the page right below it
Pointman
Pointman•3w ago
okay ill check it out Thank you mister cat
tera
tera•3w ago
😄
Pointman
Pointman•3w ago
trying dis
No description
No description
Pointman
Pointman•3w ago
boo womp oh wait no im silly yeah im silly, i shouldnt write "CategoryController" but "Category" it now works perfectly
tera
tera•3w ago
nice
Pointman
Pointman•3w ago
>looks at documentation >tag works fine "guhh? How could this be?!" pointman misadventures
tera
tera•3w ago
haha
Unknown User
Unknown User•3w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX•3w ago
If you have no further questions, please use /close to mark the forum thread as answered