在 ASP.NET MVC 中处理 post 请求
最近我开始使用 MVC,在此之前我使用“经典”ASP.NET。
使用 Ruby on Rails (RoR) 后,我想知道如何在 MVC 中实现类似于 RoR 操作的 POST 请求处理。在 RoR 中,您使用 Post
方法,因此视图只需要一个函数。
在 ASP.NET MVC 中,我需要为 GET
和 POST
使用 2 个单独的函数,因此我需要两次初始化相同的数据,并且我不喜欢重复我的代码中的某些内容。
如何在一种方法中检查请求是否为 POST
?
更新:
找到解决方案:我必须使用Request.HttpMethod。
谢谢你!
Recently I started working with MVC, before that I used "classic" ASP.NET.
After using Ruby on Rails (RoR), I wonder how to implement POST request handling in MVC similar to how RoR operates. In RoR you use the Post
method, so you need only one function for a view.
In ASP.NET MVC I need to use 2 separate functions for GET
and for POST
, so I need to initialize the same data twice, and I don't like to repeat something in my code.
How can I check if the request is POST
in one method?
Update:
Solution is found: I have to use Request.HttpMethod.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到这个问题想知道同样的事情。以下是我的情况和我使用的解决方案的详细描述(利用此处提供的其他答案)。我最初尝试使用两种单独的方法方法,但是当这些方法的方法签名变得相同时,我遇到了问题。
我有一个显示报告数据的页面。页面顶部有一个包含一些字段的表单,允许用户指定报告参数,例如开始日期、结束日期等。
我最初通过创建两个单独的方法来处理 Get 和 Post 方法来解决此问题。 post 方法会将浏览器重定向到 get 方法,以便指定的任何参数都将添加到查询字符串中,这样浏览器就不会通过对话框提示用户将重新发送他们输入的数据如果他们刷新。注意:我后来意识到,我可以通过将表单元素的方法属性设置为“Get”来完成此操作,但我认为理想情况下控制器不应该了解视图的实现方式,因此在我看来这是无关紧要的。
当我开发这两种方法时,我最终发现自己处于方法签名变得相同的情况。此外,我的这两个方法的代码变得几乎相同,因此我决定将它们合并为一个方法,并仅检查请求动词,以便当请求不是“Get”时我可以做一些稍微不同的事情。下面显示了我的两种方法的精炼示例:
为什么我接受模型对象作为 get 方法的参数?原因是我想利用模型对象中已经内置的验证逻辑。如果有人使用查询字符串中已指定的所有参数直接导航到我的页面,那么我想继续检索报告数据并将其显示在页面上。但是,如果查询字符串中指定的参数无效,那么我还希望验证错误出现在页面上。通过将我的模型对象作为参数,MVC 框架将自动尝试填充它并捕获任何验证错误,而无需我进行任何额外的工作。
我使用针对此问题发布的其他答案在我的项目中的基本控制器类上创建 RequestHttpVerb 属性:
所以最后我的综合方法如下所示:
这是我当前解决问题的方法。我不想检查 Request.HttpMethod 属性来确定是否需要执行重定向,但我没有看到问题的其他解决方案。我本来可以保留两个单独的方法来处理 Get 和 Post 请求,但相同的方法签名阻止了这种情况。我更愿意重命名我的 Post 操作处理程序方法以避免方法签名冲突,并使用某种机制向 MVC 框架指示我重命名的方法仍应处理“MyReport”操作,但我不知道有任何此类机制在MVC框架中。
I came across this question wanting to know the same thing. Below is a detailed description of my situation and the solution that I used (which utilizes the other answers provided here). I originally tried to use the two separate method approach, but I ran into a problem when the method signatures of these methods became identical.
I have a page that displays report data. At the top of the page there is a form with some fields, which allow the user to specify report parameters such as start date, end date, etc.
I originally approached this by creating two separate methods to handle the Get and the Post methods. The post method would redirect the browser to the get method so that any parameters that were specified would be added to the query string and so that the browser would not prompt the user with a dialog saying that it is going to resend the data that they entered if they refresh. Note: I realized later that I could accomplish this by setting the method attribute of my form element to "Get", but I think ideally a controller shouldn't have knowledge of how a view is implemented, so in my opinion that is irrelevant.
As I developed these two methods I eventually found myself in a situation where the method signatures became identical. Furthermore, my code for these two methods became nearly identical, so I decided to merge them into a single method and to just check the request verb so that I could do something slightly different when the request is not a "Get". A distilled example of my two methods is shown below:
Why am I accepting a model object as the parameter to my get method? The reason is that I wanted to take advantage of the validation logic already built into the model object. If someone navigates to my page directly with all parameters already specified in the query string, then I want to go ahead and retrieve the report data and display it on the page. However, if the parameters specified in the query string are invalid then I also want validation errors to appear on the page. By putting my model object as the parameter, the MVC framework will automatically attempt to populate it and will capture any validation errors without any additional work on my part.
I used the other answers posted for this question to create a RequestHttpVerb property on a base controller class in my project:
So finally my consolidated method looks like the following:
That's my current solution to the problem. I would prefer not to have to check the Request.HttpMethod property in order to determine whether I needed to perform the redirect, but I didn't see another solution to my problem. I would have been fine with keeping two separate methods to handle Get and Post requests, but the identical method signature prevented this. I would have preferred to rename my Post action handler method to avoid the method signature conflict and to use some mechanism to indicate to the MVC framework that my renamed method should still handle the "MyReport" action, but I am not aware of any such mechanism in the MVC framework.
如果 GET 和 POST 的方法签名不同,您只需要单独的方法,没有理由说一种操作方法不能处理 GET 和 POST 方法。
如果您需要知道它是 GET 还是 POST,您可以在操作中使用 Request.HttpMethod 进行检查,但我建议使用其他海报建议的用 [AcceptVerbs(HttpVerbs.Post)] 属性装饰的单独方法。
You only need separate methods for GET and POST if their method signatures differ, there's no reason why one action method can't handle GET and POST methods.
If you need to know whether it was a GET or POST, you could check using Request.HttpMethod in your action, but I would advise using a separate method decorated with the [AcceptVerbs(HttpVerbs.Post)] attribute as suggested by the other posters.
您没有签入 ASP.NET MVC。您可以使用
[AcceptVerbs(HttpVerbs.Post)]
属性修饰您的方法,以指示该方法仅适用于发布,并接受用于处理发布的方法中的模型。我强烈建议对 NerdDinner 进行演练,以了解有关 ASP.NET 的更多信息MVC 框架。
You don't check in ASP.NET MVC. You decorate your method with the
[AcceptVerbs(HttpVerbs.Post)]
attribute to indicate that the method applies to post only, and accept the model in the method used to handle the post.I'd strongly suggest doing the walkthrough for NerdDinner to understand more about the ASP.NET MVC framework.
您可以查看 Request.HttpMethod 财产。
You may take a look at the Request.HttpMethod property.
正确的方法是在 Post 请求期间使用 ModelBinding。
这样您就不必再次初始化数据。
Correct way to do is using ModelBinding during Post request.
This way you will not have to init your data again.