我设法让它工作。这是我的做法。
我首先将这一行添加到 Web.config 文件中:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
这允许 .pdf、.png 等中的点字符在 url 中。
我在 RouteConfig.cs 中为控制器添加了新路由。
routes.MapRoute(
name: "Material",
url: "Material/Download/{file}",
defaults: new { controller = "Material", action = "Download", file = UrlParameter.Optional }
);
我创建了一个新的控制器“Material”。
// GET: Material
[Authorize]
public ActionResult Download(string file)
{
string path = Server.MapPath(String.Format("~/App_Data/Material/{0}", file));
if(System.IO.File.Exists(path))
{
string mime = MimeMapping.GetMimeMapping(path);
return File(path, mime);
}
return HttpNotFound();
}
并且还转移了app_data里面的material文件夹。
这似乎工作得很好。只有授权用户才能访问材料文件夹。