使用貼here博客和,所以我已經創建了一個控制器,它應該處理我所有的錯誤頁面的主題here。MVC3自定義錯誤頁給出空白結果
在我的Global.asax.cs我已經得到了下面的一段代碼:
protected void Application_Error()
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
var routeData = new RouteData();
Response.Clear();
Server.ClearError();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
// Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
IController errorsController = new ErrorController();
HttpContextWrapper wrapper = new HttpContextWrapper(Context);
var rc = new RequestContext(wrapper, routeData);
errorsController.Execute(rc);
}
我ErrorController看起來是這樣的:
public class ErrorController : BaseController
{
/// <summary>
/// Returns a default view for not having access.
/// </summary>
public ActionResult Unauthorized()
{
BaseModel viewModel = new BaseModel
{
LoginModel = new LogonModel(),
ProfessionsTopX = GetTopXProfessions()
};
return View(viewModel);
}
public ActionResult General(Exception exception)
{
return View("Exception", exception);
}
public ActionResult Http404()
{
//This line works
//return Content("Not found", "text/plain");
//This line presents a blank page
return View("404","_Layout");
}
public ActionResult Http403()
{
return View("403", "_Layout");
}
}
我的Razor視圖只包含一張下面的HTML;
@{
ViewBag.Title = "404";
}
<h2>404</h2>
This is a 404 page!
當我使用返回內容我得到一個純textoutput告訴我,我看404頁。不過,我希望404頁面適合我設計的其餘部分,所以我想使用我自己的Views。但是,一旦我使用返回視圖,我得到一個空白頁面。我希望錯過一些非常明顯的東西,但我沒有看到它。
這同樣的事情發生在我身上,直到後來我模擬我的代碼的帖子。 –
這件事情發生在我身上。你有沒有找到這個問題的解決方案? –