0

So, I wanted to apply some friendly error pages to an existing app; due to some recent Azure issues revealing the standard .NET "yellow page of death" too many times. I used this example: Displaying custom error CSHTML page in MVC5 on 404 / 500 / any exception?

Everything seems to work okay (in respect of the error code picking up the correct view), but the resulting response is the full HTML as text. The page isn't rendering... This appears to be for Chrome, Edge works fine. I'm assuming it isn't a browser specific issue, but something I've inadvertantly introduced.

Any ideas how I can force this page to render? Am I missing a specific 'redirect' action rather than a "ReWrite"; I've already checked redirectMode="ResponseRedirect".

Web.config:

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRedirect">
      <error redirect="~/Error/HttpError404" statusCode="404"/>
      <error redirect="~/Error/HttpError500" statusCode="500"/>
    </customErrors>
  </system.web>

ErrorController:

namespace RMS.Web.Controllers.Web
{
    using System.Web.Mvc;

    public class ErrorController : Controller
    {
        public ActionResult Error()
        {
            return View();
        }
        public ActionResult HttpError404()
        {
            Response.StatusCode = 200;
            return View();
        }
        public ActionResult HttpError500()
        {
            Response.StatusCode = 200;
            return View();
        }
    }
}

Standard View (Each view is a slight variation on these)

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

<div class="error">
    <div class="error-code m-b-10 m-t-20">Error 404<i class="fa fa-warning"></i></div>
    <h3 class="font-bold">We couldn't find the page..</h3>

    <div class="error-desc">
        Sorry, but the page you are looking for was either not found or does not exist. <br />
        Try refreshing the page or click the button below to go back to the Homepage.
        <div>
            <a class="login-detail-panel-button btn" href="/Home/Index">
                <i class="fa fa-arrow-left"></i>
                Go back to Homepage
            </a>
        </div>
        <div>
            @*@Model.Exception*@
        </div>
    </div>
</div>

Browser wraps content in < pre > tags

Community
  • 1
  • 1
PraytaPi
  • 3
  • 3
  • Can you show us your config entries and actual code? – ramiramilu Sep 16 '16 at 08:37
  • I tried your code and it is absolutely working fine for me in brand new MVC project. Can you see Chrome F12 Network tab and check if there is anything missing or error? – ramiramilu Sep 16 '16 at 09:00
  • Chrome seems to wrap it in a PRE tag. So it shows the HTML content as text... From what I've seen, Firefox has similar behaviour. I'll continue to look into why the content isn't being returned as HTML. – PraytaPi Sep 16 '16 at 09:17
  • Can you try by adding - `Response.ContentType = "text/html";` in your action and see if it works? – ramiramilu Sep 16 '16 at 09:20
  • Yes, that's exactly what I tried and it worked! Great minds and all that. THANK YOU for being a great help! – PraytaPi Sep 16 '16 at 09:24

0 Answers0