2014-12-05 80 views
0

我有一个网站,提供媒体文件。如果没有找到一个文件,我在页面上显示一个简单的信息与下面的代码的用户:显示HTML格式的错误消息与HttpResponseMessage

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

的问题是,是,它是一个白色的页面不显示HTML上只是简单的文本我希望它能够显示HTML。

有没有办法做到这一点?

谢谢!

回答

1

你可能不会在客户端发送了正确的ContentType标头,并有可能需要设置HttpResponse.ContentType字符串:

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

希望这有助于!

UPDATE:如果你需要支持UTF8,试试这个:

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