ASP.NET MVC确定由文件/文件路径MIME类型 [英] ASP.NET MVC Determine mime type by file/file path

查看:160
本文介绍了ASP.NET MVC确定由文件/文件路径MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 3。
该应用程序是针对.NET 4.0。

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

我试图通过一个虚拟的路径作为参数传递给我的控制器的行动,并没有仔细检查其服务器上的实际物理路径,并提供基本的授权返回文件。不幸的是我无法找到文件的方法来生成相应的ActionResult合适的超载。我被困在确定MIME类型的文件。

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.

我发现一对夫妇通过文件名确定MIME类型的解决方案,但他们都不满足我。我想知道如何IIS确定MIME类型时,您所请求的服务器上的文件?是否有可能以某种方式传递给IIS的责任确定的MIME类型的响应

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

推荐答案

如果您使用的是.NET 4.5有一个函数,现在需要一个文件名并返回一个MIME类型:

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

MimeMapping.GetMimeMapping(filename)

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping.aspx\">http://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping.aspx

在这个问题以后再看看功能:

Take a look at the function later in this question:

<一个href=\"http://stackoverflow.com/questions/636179/how-to-use-generic-handlers-ashx-in-asp-net-mvc/636720#636720\">How在ASP.NET MVC中使用通用处理程序(ASHX)?

这是过时与现在的文件类型列表中的一点点,但它是一个开始,将设法找到我最近的之一。

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.

这篇关于ASP.NET MVC确定由文件/文件路径MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆