2016-09-25 43 views
0

我在Global.asax中C#MVC的Application_Error重定向页面而不是HTML(这似乎是字符串)。

我把错误创建的页面的Application_Error。 而我正在重定向。

重定向URL = xxxxx.com/Error/E404

但页面的源代码显示。

enter image description here

ErrorController内容;

public class ErrorController : Controller 
{ 
    // GET: Error 
    public ActionResult E404() 
    { 
     var res = HttpContext.Response; 
     return View(); 
    } 
} 

Global.asax content;

protected void Application_Error() 
    { 
     var exception = Server.GetLastError(); 
     var httpException = exception as HttpException; 
     var request = Request; 
     Response.Clear(); 
     Server.ClearError(); 
     Response.TrySkipIisCustomErrors = true; 
     var routeData = new RouteData(); 
     routeData.Values["controller"] = "Error"; 
     routeData.Values["action"] = "Index"; 
     routeData.Values["exception"] = exception; 
     Response.StatusCode = 500; 

     if (httpException != null) 
     { 
      Response.StatusCode = httpException.GetHttpCode(); 

      switch (Response.StatusCode) 
      { 
       case 403: 
        routeData.Values["action"] = "E403"; 
        break; 
       case 500: 
        routeData.Values["action"] = "E500"; 
        break; 
       case 404: 
        routeData.Values["action"] = "E404"; 
        break; 
       default: 
        routeData.Values["action"] = "General"; 
        break; 
      } 

      // -- Save Log + 
      string Url = request.Url.OriginalString; 
      string Query = request.Url.Query; 
      string ReferrerUrl = request.UrlReferrer == null ? null : request.UrlReferrer.ToString(); 
      string Browser = request.Browser.Browser; 
      string UserAgent = request.UserAgent; 
      string IpAddress = request.UserHostAddress; 
      string iPv6Address = ""; 
      bool IsMobileDevice = request.Browser.IsMobileDevice; 

      ActionLogger.Log_ApplicationError(Response.Status, 
       Response.StatusCode.ToString(), 
       Response.StatusDescription, 
       exception.HResult.ToString(), 
       exception.HelpLink, 
       exception.TargetSite.ToString(), 
       exception.Source, 
       exception.StackTrace, 
       Url, 
       Query, 
       DateTime.Now, 
       exception.Message, 
       IpAddress, 
       iPv6Address, 
       ReferrerUrl, 
       Browser, 
       UserAgent, 
       IsMobileDevice); 
      // -- Save Log 
     } 
     IController errorsController = new Controllers.ErrorController(); 
     var rc = new RequestContext(new HttpContextWrapper(Context), routeData); 
     errorsController.Execute(rc); 
    } 

    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     WebApiConfig.Register(GlobalConfiguration.Configuration); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
     GlobalConfiguration.Configuration.EnsureInitialized(); 

     RegisterViewEngine(ViewEngines.Engines); 

ThemeApplicationBuild.RegisterApplication(this);

}

...

+0

你在客户端上看到的HTTP响应头? – SLaks

+0

ContentType text/html 请参阅Application_Error:http://i.hizliresim.com/ZYz9mZ.png 请参阅ErrorController:http://i.hizliresim.com/J3zQPQ.png –

回答

0

我解决了这个问题。

的Application_Error设置的ContentType = “text/html的”

 HttpContext.Current.Response.ContentType = "text/html"; 

     IController errorsController = new Controllers.ErrorController(); 
     var rc = new RequestContext(new HttpContextWrapper(Context), routeData); 
     errorsController.Execute(rc); 
相关问题