【发布时间】:2015-08-20 23:07:05
【问题描述】:
我首先创建了一个 UrlHelperExtension 类来构建我的 url,就像这样
"/Home/DownloadFiles?directory="+directory+"&filename"+HttpUtility.UrlEncode(filename)
但我想为什么不使用 mvc 来构建我的 URL,所以在我看来我做到了
href="@Url.Action("DownloadFiles","Home", new {directory = "files", filename="1.mp3"}"
但输出 /Home/DownloadFiles/files/1.mp3 找不到文件(我得到 404)。我的操作方法是
public ActionResult DownloadFiles(string directory, string filename){
//log which file is downloaded by who
//Add download header content-disposition attachement
//Send response with the file
return null;
}
我唯一的路线是这样的
routes.MapRoute(
name:"Default",
url: "{controller}/{action}/{directory}/{filename}",
defaults: new {controller = "Home", action = "Index", directory = UrlParameter.Optional, filename = UrlParameter.Optional}
)
我认为我在真正理解路线方面存在一些问题,因为我不确定如何解决这个问题,所以我不必使用我的扩展类,它真的没有多大作用。也许我不应该使用 URL.Action ?将 Url.Action UrlEncode 文件名参数?目录参数只有 1 个“深”,所以它不能是 abc/def 只有 abc,我添加了相关部分,所以我不担心 UrlEncoding 它。
【问题讨论】:
-
~/Home/DownloadFiles/files/1.mp3将为您返回 200 OK? -
为您解答。我也得到一个404。他试图在错误的地方寻找文件。我需要它用正确的参数调用我的操作方法,而不是在 /Home/DownloadFiles/files 中查找 obv 不存在的文件。
标签: asp.net-mvc asp.net-mvc-4 routes razor-2