如果您重定向,则无法发送复杂对象。我的意思是你可以使用Session
和TempData
(它在幕后使用会话),但它是丑陋的,我会建议反对。
如果你想复杂的对象不重定向。设置适当的状态代码和转让这是更为REST风格和搜索引擎友好:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
Context.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "error";
routeData.Values["action"] = "Http500";
routeData.Values["exception"] = exception;
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
其中ErrorController看起来是这样的:
public class ErrorController : Controller
{
public ActionResult Http500(Exception exception)
{
Response.StatusCode = 500;
// TODO: do something with the exception like logging it
// and render some view explaining the annoyed user
// that something very wrong happened to your application
// which wasn't your fault of course or something
}
}
如果你重定向你不能发送复杂的对象。
你可以把它放在tempdata中吗?通常我只是记录异常并使用日志来调试 – 2011-02-07 21:56:08
@Phil,`TempData`和重定向来处理异常? 302状态码而不是500?不感觉非常RESTful或SEO友好。 – 2011-02-07 22:02:53