Ale*_*lex 4 .net c# asp.net-mvc post httprequest
最近我开始使用MVC,之前我使用了"经典"ASP.NET.
在使用Ruby on Rails(RoR)之后,我想知道如何在MVC中实现类似于RoR操作的POST请求处理.在RoR中,您使用该Post方法,因此您只需要一个视图功能.
在ASP.NET MVC我需要使用2个独立的功能GET和POST,所以我需要初始化两次相同的数据,我不喜欢重复的东西在我的代码.
如何检查请求是否POST在一种方法中?
更新:
找到解决方案:我必须使用Request.HttpMethod.
谢谢!
我遇到了这个想要知道同样事情的问题.下面是我的情况和我使用的解决方案的详细描述(利用此处提供的其他答案).我最初尝试使用两种单独的方法方法,但是当这些方法的方法签名变得相同时,我遇到了问题.
我有一个显示报告数据的页面.在页面顶部有一个包含一些字段的表单,允许用户指定报告参数,如开始日期,结束日期等.
我最初通过创建两个单独的方法来处理Get和Post方法来解决这个问题.POST方法将浏览器重定向到get方法,使中指定的任何参数将被添加到查询字符串,这样浏览器就不会出现一个对话框说,它会重新发送,他们输入的数据提示用户如果他们刷新 注:后来我意识到,我可以通过设置我的表单元素,以"获取"的方法属性做到这一点,但我认为最好的控制不应该有一个观点是如何实现的知识,所以在我看来那是无关紧要的.
当我开发这两种方法时,我最终发现自己处于方法签名变得相同的情况.此外,我的这两个方法的代码变得几乎相同,所以我决定将它们合并成一个单一的方法和刚刚检查请求的动词,这样我可以做一些稍微不同,当请求不是"获取".我的两种方法的蒸馏示例如下所示:
// this will not compile because the method signatures are the same
public ActionResult MyReport(DateRangeReportItem report)
{
// if there are no validation errors and the required report parameters are completed
if (ModelState.IsValid && report.ParametersAreComplete)
{
// retrieve report data and populate it on the report model
report.Result = GetReportData(report.CreateReportParameters());
}
return View(report);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MyReport(DateRangeReportItem report)
{
if (ModelState.IsValid && report.ParametersAreComplete)
{
// redirect to the same action so that if the user refreshes the browser it will submit a get request instead of a post request
// this avoids the browser prompting the user with a dialog saying that their data will be resubmitted
return RedirectToAction("MyReport", new { StartDate = report.StartDate, EndDate = report.EndDate });
}
else
{
// there were validation errors, or the report parameters are not yet complete
return View(report);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我接受模型对象作为我的get方法的参数?原因是我想利用已经内置到模型对象中的验证逻辑.如果有人直接使用查询字符串中已指定的所有参数导航到我的页面,那么我想继续检索报告数据并将其显示在页面上.但是,如果查询字符串中指定的参数无效,那么我还希望在页面上显示验证错误.通过将我的模型对象作为参数,MVC框架将自动尝试填充它并捕获任何验证错误,而无需我做任何额外的工作.
我使用为此问题发布的其他答案在我的项目中的基本控制器类上创建RequestHttpVerb属性:
public HttpVerbs RequestHttpVerb
{
get { return (HttpVerbs)Enum.Parse(typeof(HttpVerbs), this.Request.HttpMethod, true); }
}
Run Code Online (Sandbox Code Playgroud)
所以最后我的整合方法如下所示:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult MyReport(DateRangeReportItem report)
{
// check if there are any validation errors in the model
// and whether all required report parameters have been completed
if (ModelState.IsValid && report.ParametersAreComplete)
{
// this is unnecessary if the form method is set to "Get"
// but within the controller I do not know for sure if that will be the case in the view
if (HttpVerbs.Get != this.RequestHttpVerb)
{
// redirect to the same action so that if the user refreshes the browser it will submit a get request instead of a post request
// this avoids the browser prompting the user with a dialog saying that their data will be resubmitted
return RedirectToAction("MyReport", new { StartDate = report.StartDate, EndDate = report.EndDate });
}
// there were no validation errors and all required report parameters are complete
// retrieve report data and populate that data on the model
report.Result = GetReportData(report.CreateReportParameters());
}
// display the view with the report object
// Any model state errors that occurred while populating the model will result in validation errors being displayed
return View(report);
}
Run Code Online (Sandbox Code Playgroud)
这是我目前解决问题的方法.我宁愿不必检查Request.HttpMethod属性以确定是否需要执行重定向,但我没有看到我的问题的另一个解决方案.我可以保持两个单独的方法来处理Get和Post请求,但是相同的方法签名阻止了这一点.我本来希望重命名我的Post动作处理程序方法以避免方法签名冲突并使用某种机制向MVC框架指示我的重命名方法仍然应该处理"MyReport"动作,但我不知道任何这样的机制在MVC框架中.
如果他们的方法签名不同,你只需要单独的GET和POST方法,没有理由为什么一个动作方法不能处理GET和POST方法.
如果您需要知道它是GET还是POST,您可以在动作中使用Request.HttpMethod进行检查,但我建议使用其他海报建议的[AcceptVerbs(HttpVerbs.Post)]属性修饰的单独方法. .
| 归档时间: |
|
| 查看次数: |
20129 次 |
| 最近记录: |