2

I am using MVC 3 Razor, and i am getting a "save as" dialog with a "unknown file type" and the content is the Html Page once in a while on multiple pages on the site, this dialog shows on IE, and on Firefox it shows an Error page that says "XML Parsing Error" .

sometimes when the "save as" appears, i checked the response headers and it shows that the "Content-type" of the html page is "application/xhtml+xml; charset=utf-8", i did not set of these headers on the server side.

The problem is that this is not happening all the time, it comes and goes, and not on a specific page.

I am not using any ajax on the site, and also i tried to reinstall aspnet as per some other threads, i have also checked this thread regarding response headers, but i still get this problem.

Note: i am using an Image Controller with an ActionResult to return images from the DB as a File Like So

Function GetImage(ByVal id As Integer?) As ActionResult


        Dim record = rep.GetArticlePhoto(id)


        Return MyBase.File(record.ArticlePhotoContent.ToArray, "image/jpeg")



End Function
Community
  • 1
  • 1
Buaziz
  • 81
  • 6
  • Sorry, there isn't enough information here to answer you. Probably you are returning text/xml as the content type somehow. But there isn't enough information for me to even guess how that is happening. – Ariel Jun 23 '11 at 09:34
  • i just need some pointers to where to look for problems like this. – Buaziz Jun 23 '11 at 09:37
  • Install Fiddler and with it running connect to the site again, you'll be able to see exactly what is being sent back and forth and that will tell you why IE thinks its a file download. The MIME type is probably wrong somehow. With that info we'll be able to help you fix it. – Tridus Jun 23 '11 at 10:01
  • i checked with fiddler, i get response "Content-type" as "application/xhtml+xml; charset=utf-8" BUT only sometimes. and other times its "text/html; charset=utf-8" – Buaziz Jun 23 '11 at 10:13

2 Answers2

2

it turns out that when you use

MyBase.File(img.GetBytes, "image/jpeg")

it will return a FileContentResult

and if you use

MyBase.File(Server.MapPath("~/content/images/na.png"), "image/png")

it will use the other overload which returns a different object type, a FilePathResult

different overloads return different object types - That's how i missed it

it seems that others had some problems with FilePathResult, so i swapped it, and for now it works fine.

Community
  • 1
  • 1
Buaziz
  • 81
  • 6
0

The most likely reason as to why it's xhtml+xml sometimes is that your page is generating an error and returning the default .net exception page. This could happen because ArticlePhotoContent is Nothing when you use the .ToArray() or possibly even record is Nothing.

I would modify your function like so.

Function GetImage(ByVal id As Integer?) As ActionResult
    Dim record = rep.GetArticlePhoto(id)
    If(record IsNot Nothing AndAlso record.ArticlePhotoCount IsNot Nothing) Then
        Return MyBase.File(record.ArticlePhotoContent.ToArray, "image/jpeg")
    Else
        Return New HttpNotFoundResult()
    End If
End Function

However, this does not take into account that rep.GreatArticlePhoto might cause an exception. You could wrap it in a try-catch block.

Buildstarted
  • 26,529
  • 10
  • 84
  • 95
  • actually the whole method has multiple try catch, and validation procedures, but i removed the code for simplicity. but am going to investigate your thought on returning errors, you might gave me a clue there. – Buaziz Jun 23 '11 at 15:48
  • do you guys really think that "Content-type" keeps changing to "xhtml+html" from the Image Controller, or could it be something else am missing ? , [here is the site](http://www.kathima.com/) but the error only appears about 3% of the page views, so you might not see it. – Buaziz Jun 23 '11 at 15:50