C# MVC-如何避免操作返回404(如果不是POST)
我在控制器中有两个操作:C# MVC-如何避免操作返回404(如果不是POST),c#,asp.net-mvc,http-post,C#,Asp.net Mvc,Http Post,我在控制器中有两个操作: public ActionResult ReportRequest() { return View(); } [HttpPost] public ActionResult Report(ReportRequestObj rr, FormCollection formValues) { if (Request.HttpMethod != "POST") return
public ActionResult ReportRequest()
{
return View();
}
[HttpPost]
public ActionResult Report(ReportRequestObj rr, FormCollection formValues)
{
if (Request.HttpMethod != "POST")
return RedirectToAction("ReportRequest");
ReportingEngine re = new ReportingEngine();
Report report = re.GetReport(rr);
return View(report);
}
我的问题是,“报告”页面的URL会保存在浏览器中,当我单击它时,会得到404,因为请求尚未发布
我在中添加了一个处理程序来重定向到报告请求页面,但是在调试时,它似乎根本没有达到这个目的
是否有其他方法可以确定请求是否是帖子,如果不是重定向回另一个页面
谢谢添加操作
public ActionResult Report()
{
return RedirectToAction("ReportRequest");
}
或者只需从操作
报告中删除[HttpPost]
,您无法在此处处理404错误,因为该请求永远不会到达您的操作。您正在使用一个动作过滤器,确保只有POST请求到达该代码段
您应该创建另一个操作方法来响应GET请求,并在其中返回视图
[HttpGet]
public ActionResult Report()
{
return View("ReportRequest");
}
删除方法顶部的[HttpPost],您可以使用您的if(Request.HttpMethod!=“POST”)检查返回重定向到操作(“ReportRequest”);它是否应该返回。