【发布时间】:2014-10-30 22:13:45
【问题描述】:
简而言之,有没有办法让 MVC Upload ActionResult & Download FileResult 遵守 web.config 的禁止 MIME 类型?
我看到一些解决方案主要集中在 ActionResult 中的 MIME 类型的硬编码,方法是在上传时检查 HttpPostedFileBase 并在下载时提供 MIME 类型,但我正在寻找一种更优雅的方法。硬编码,甚至用 appsettings 重新发明轮子似乎很愚蠢。
我已将以下内容放入 web.config 无济于事:
<httpHandlers>
<add verb="*" path="*.exe" type="System.Web.HttpForbiddenHandler" />
</httpHandlers>
MVC 只是不尊重 IIS 在 MIME 类型上的配置,而是自行其是,令人沮丧。有谁知道使用 config 通过 ASP.NET MVC 完成此任务的更好方法?否则,我可能会求助于带有 appsettings 的操作过滤器来控制它,或类似的。
下载代码,只是为了展示提供文件的方法:
/// <summary>
/// Downloads the document.
/// </summary>
/// <param name="documentId">The document identifier.</param>
/// <returns></returns>
public ActionResult DownloadDocument(int documentId)
{
ActionResult actionResult = null;
InternalClient internalClient = null;
Document document = null;
ContentDisposition contentDisposition = null;
try
{
internalClient = new InternalClient();
document = internalClient.GetDocument(documentId);
contentDisposition = new ContentDisposition
{
FileName = document.FileName,
Inline = false,
};
Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
actionResult = File(document.FileBytes, document.ContentType);
}
catch (Exception exception)
{
exception.Log();
}
finally
{
internalClient.TryDispose();
}
return actionResult;
}
【问题讨论】:
-
在 System.webServer->handlers 上尝试相同的操作,这就是我拥有文件扩展处理程序的地方。
-
我只是没有成功,很遗憾。感谢您的提示。
标签: c# asp.net-mvc file-upload web-config mime-types