简体   繁体   English

MVC将渲染视为原始HTML

[英]MVC View rendering as raw HTML

I use this code in my Global.asax file to catch all 404 errors and redirect them to a custom controller/view. 我在我的Global.asax文件中使用此代码来捕获所有404错误并将它们重定向到自定义控制器/视图。

    protected void Application_Error(object sender, EventArgs e) {
        Exception exception = Server.GetLastError();

        Response.Clear();
        HttpException httpException = exception as HttpException;
        if (httpException != null) {
            if (httpException.GetHttpCode() == 404) {
                RouteData routeData = new RouteData();
                routeData.Values.Add("controller", "Error");
                routeData.Values.Add("action", "Index");

                Server.ClearError();

                IController errorController = new webbage.chat.Controllers.ErrorController();
                Response.StatusCode = 404;
                errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
            }
        }
    }

I have three controllers for my application at the moment, Users , Rooms and Home 我目前有三个控制器用于我的应用程序, UsersRoomsHome

When I type in something like {localhost}/rooms/999 (this would cause it to throw a 404 since 999 is an invalid room Id), it redirects and renders just fine, everything works as expected. 当我输入像{localhost}/rooms/999这样的东西时(这会导致它抛出404,因为999是一个无效的房间ID),它会重定向并渲染得很好,一切都按预期工作。

However if I type in an invalid controller name like so {localhost}/test it redirects it to the view like it should, but when it renders it's just the HTML as plain text. 但是,如果我键入一个像{localhost}/test这样的无效控制器名称,它会将它重定向到视图,但是当它呈现它时,它只是HTML作为纯文本。 Can someone point out why it would do that? 有人能指出为什么会这样做吗?

Here's my ErrorController 这是我的ErrorController

public class ErrorController : Controller {
    public ActionResult Index() {
        return View();
    }

    public ActionResult NotFound() {
        return View();
    }

    public ActionResult Forbidden() {
        return View();
    }
}

And my view: 我的观点是:

@{
    ViewBag.Title = "Error";
}

<div class="container">
    <h1 class="text-pumpkin">Ruh-roh</h1>
    <h3 class="text-wet-asphalt">The page you're looking for isn't here.</h3>
</div>

edit 编辑

I ended up going with just web.config error handling since it's much simpler I suppose. 我最后只使用web.config错误处理,因为我认为它更简单。 I removed the Application_Error code from my Global.asax file and just put this snippet in my web.confg file 我从Global.asax文件中删除了Application_Error代码,并将此代码段放在我的web.confg文件中

  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">      
      <remove statusCode="403"/>
      <remove statusCode="404"/>
      <remove statusCode="500"/>
      <error statusCode="403" responseMode="ExecuteURL" path="/Error/Forbidden" />
      <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
      <error statusCode="500" responseMode="ExecuteURL" path="/Error" />
    </httpErrors>
  </system.webServer>

I'd still love to know why this is occurring though. 我仍然很想知道为什么会这样。

You can try explicitly setting the ContentType in the action: 您可以尝试在操作中显式设置ContentType:

public ActionResult NotFound() {
    // HACK: fix rendering raw HTML when a controller can't be found 
    Response.ContentType = "text/html";
    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM