13

I am using ASP.NET MVC 3. The application is targeting .net 4.0.

I was trying to pass a virtual path as argument to my controller's action and return the file without reviewing the actual physical path on the server and providing basic Authorization. Unfortunately I was unable to find a suitable overload for File method to generate the appropriate ActionResult. I am stuck at determining mime type for the file.

I've found a couple of solutions for determining mime-types by file name, but none of them are satisfying me. I was wondering how does IIS determines the mime type when you are requesting a file on the server? Is it possible to somehow pass to IIS the responsibility for determining the mime type for the response

1

1 Answer 1

31

If you are using .net 4.5 there is a function now that takes a filename and returns a mimetype:

MimeMapping.GetMimeMapping(filename)

http://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping.aspx

Take a look at the function later in this question:

How to use Generic Handlers (ASHX) in ASP.NET MVC?

It is a little out of date with the list of file types now but it's a start, will try to find my more recent one.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes thank you. I found this new cool feature. Unfortunately the application is targeting .net 4.0. Editing my question...
I do have a mime type from file ending function too if you want a copy. will have to dig out
I would really appreciate this.
Ok added reference to an answer I have given before which has an older version of the function there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.