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