【问题标题】:C# MVC Application_Error Redirect Page Not Html (It seems to be the string.)C# MVC Application_Error 重定向页面不是 Html(它似乎是字符串。)
【发布时间】:2016-09-25 18:54:50
【问题描述】:

我在 Global.asax 中创建了页面 Application_Error

我犯了错误。 我正在重定向。

重定向网址 = xxxxx.com/Error/E404

但页面的源代码显示。

ErrorController 内容;

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

Global.asax 内容;

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);

}

...

【问题讨论】:

标签: c# asp.net-mvc application-error


【解决方案1】:

我解决了这个问题。

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    相关资源
    最近更新 更多