ASP.NET Core: redirect from GET to POST

前端 未结 2 1241
广开言路
广开言路 2021-01-26 05:56

I want to call MarriageById as GET, like this:

var url = \'/MarriageById?id=\' + id;

But I also want to have a single Action

相关标签:
2条回答
  • 2021-01-26 05:59

    Using RedirectToAction always implies a GET, so this won't work to reach the Marriage action method that only accepts POST.

    However there is nothing wrong with calling the other method yourself, it is still a method like any other. So try this instead:

    return Marriage(marriage);
    

    And on a side note: if the Marriage method will always only be used to display data, and never to save, store or change data, then using POST is not the best choice. POST typically implies a call with side effects (save, store, change, or even delete), and in general it is best to stick to that convention.

    0 讨论(0)
  • 2021-01-26 06:23

    I don't think you should mix GET with POST verbs. They are just semantically different. If you have a similar functionality that you want to execute for those 2 methods, maybe instead of calling POST from GET you might want to extract the common parts ot some other 'private' method or even layer (depending on the use case).

    Hope it makes sense

    0 讨论(0)
提交回复
热议问题