CommonController > PageNotFound > HTML source code shown due to missing Content Type

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
Due to missing Content Type in some rare cases, the PageNotFound action of CommonController is showing HTML code instead of a rendered html page. If I add ContentType manually in PageNotFound action everything works fine.

As far as I can tell, this happens when calling a unknown route.
7 years ago
Hi Sven,

It works fine on our demo site (test - http://demo.nopcommerce.com/dfgdfg/dfg). How can I reproduce it (on our demo site)?
7 years ago
BTW, do you experience it during development (in VS) or on a live site?
7 years ago
a.m. wrote:
BTW, do you experience it during development (in VS) or on a live site?


both.
7 years ago
a.m. wrote:
BTW, do you experience it during development (in VS) or on a live site?


Okay, I have to add that we want to redirect links from our old shop that used classic asp with .asp extension in URL. So I added a IIS handler for that in web.config:

<remove name="ASPClassic" />
<add name="ASPClassicFileHandler" path="*.asp" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />


Now if I try to request a URL like http://localhost/products/12345/myproduct.asp I configured a route for that and everything works (mapping old Id to new Id, getting slug and redirecting).

If I call a URL that does not match the route but ends with .asp (http://localhost/de/test.asp), it will redirect to 404 (CommonController, NotFound) as expected but the page will show html content instead of rendering it.

Maybe this is related with the TransferRequestHandler? Than it is no general bug. I fixed it by adding "this.Response.ContentType = "text/html; charset=utf-8";"

        //page not found
        public ActionResult PageNotFound()
        {
            this.Response.ContentType = "text/html; charset=utf-8";
            this.Response.StatusCode = 404;
            this.Response.TrySkipIisCustomErrors = true;

            return View();
        }
7 years ago
Thanks, Sven!

Here is a work item

What is your site URL?
7 years ago
We fixed it now.
You can see changes in this commit.
Please test it and let us know if you still have any problems.
7 years ago
Works. Thank you.
7 years ago
I've also seen this behaviour on v3.60 and used the same fix of adding the content-type to the PageNotFound method:

public ActionResult PageNotFound()
{
    this.Response.StatusCode = 404;
    this.Response.TrySkipIisCustomErrors = true;
    this.Response.ContentType = "text/html"; //Prevent missing content-type response header on page not found

    return View();
}

It happens in development through IIS Express or local IIS and also on the live site (or would if I hadn't fixed it by adding the ContentType manually). I haven't been able to reproduce it on the nop demo site though so I'm not sure what the difference is, though I suspect it's something to do with the IIS error handling.

It does seem to depend on how many parts are in the url so must be related to the route handling somehow:

1. http://localhost:15536/unknown shows the Page not found page and correctly sets the content-type in the header

2. http://localhost:15536/unknown/unknown or http://localhost:15536/unknown/unknown/unknown shows the Page not found page but doesn't set a content-type header so the html source code is displayed as plain text

3. http://localhost:15536/unknown/unknown/unknown/unknown shows the standard IIS 404 page (this last one can be replicated on the nop demo site: http://demo.nopcommerce.com/unknown/unknown/unknown/unknown).

Edit: I've just noticed that Romanov has posted the same fix since I started typing this, but the 3rd example is still valid.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.