SkyeBoniwell
SkyeBoniwell

Reputation: 7102

show HTML formatted error message with HttpResponseMessage

I have a website that serves up media files. If a file is not found, I show a simple message on the page to the user with the code below:

        var fileNotFoundResponse = new HttpResponseMessage(HttpStatusCode.NotFound);

        if (!File.Exists(mediaFile.FilesystemLocation))
        {
            fileNotFoundResponse.Content = new StringContent("File not found! <br /> Please contact <a href='mailto:[email protected]'>[email protected]</a>");
            return ResponseMessage(fileNotFoundResponse);
        }

The problem is, is that it's just plain text on a white page that doesn't show HTML and I want it to be able to display the HTML.

Is there a way to do this?

Thanks!

Upvotes: 0

Views: 292

Answers (1)

user4099684
user4099684

Reputation:

You probably aren't sending the client the correct ContentType header, and likely need to set the HttpResponse.ContentType string:

fileNotFoundResponse.ContentType = "text/html";
fileNotFoundResponse.Clear();
fileNotFoundResponse.BufferOutput = true;

Hope this helps!

UPDATE: If you need UTF8 support, try this:

fileNotFoundResponse.ContentType = "text/html; charset=utf-8";

Upvotes: 1

Related Questions