如何在get和post方法中抛出HttpException并获得相同的结果?
我正在ASP.NET MVC 4
上使用IIS 7.5
。我试图在HttpException
和HttpGet
操作方法中计算关于抛出HttpPost
的跟随方案。我希望有人可以帮助分享他们对此事的看法。
我有以下2种动作方法。这个处理HttpGet:
public ActionResult Logon()
{
throw new HttpException(401, "brendan's test message");
}
这个处理HttpPost:
[HttpPost]
public ActionResult Logon(LogonViewModel model, string returnUrl)
{
throw new HttpException(401, "brendan's test message");
}
这是我在global.asax文件中处理错误的方法:
protected void Application_Error()
{
Exception exception = Server.GetLastError();
HttpException httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
RouteData routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "Http500";
routeData.Values["exception"] = httpException;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 401:
routeData.Values["action"] = "Http401";
break;
case 403:
routeData.Values["action"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
IController errorController = new ErrorController();
RequestContext requestContext = new RequestContext(new HttpContextWrapper(Context), routeData);
errorController.Execute(requestContext);
}
这是我的ErrorController类:
public class ErrorController : Controller
{
public ActionResult Http401()
{
return View();
}
public ActionResult Http500()
{
return View();
}
}
我的web.config
中没有配置任何错误。
这是我的看法。我已经取出了文本框:
@using (Html.BeginForm("Logon", "Account", FormMethod.Post, new { role = "form" }))
{
<p class="buttons">
<button type="submit" class="btn btn-primary">Logon</button>
</p>
}
我想要实现的是将当前错误页面提供给当前URL。
当我运行HttpGet操作方法时,我的错误页面显示完美。当我点击按钮进入HttpPost动作方法时,没有任何反应。我期待401错误页面显示,但它没有。只显示当前页面。流程与get方法相同,它一直到错误控制器的视图方法。
在get和post方法中抛出HttpException有什么区别?