【发布时间】:2013-01-25 12:15:49
【问题描述】:
用户可以下载位于文件夹PriceInformations 中的价格信息PDF,其中包含指定文档类型的子文件夹,例如:
/PriceInformations/Clothes/Shoes.pdf
/PriceInformations/Clothes/Shirts.pdf
/PriceInformations/Toys/Games.pdf
/PriceInformations/Toys/Balls.pdf
考虑在 Controller Document 中执行以下操作以下载这些 PDF:
// Filepath must be like 'Clothes\Shoes.pdf'
public ActionResult DownloadPDF(string filepath)
{
string fullPath = Path.Combine(MyApplicationPath, filepath);
FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
return base.File(fileStream, "application/pdf");
}
为了获取 PDF 文档,我的客户希望 URL 类似于:
/PriceInformations/Clothes/Shoes.pdf
我可以轻松地为这种情况创建一个重载函数:
public ActionResult DownloadPDF(string folder, string filename)
{
return this.DownloadPDF(Path.Combine(folder, filename);
}
像这样映射它
routes.MapRoute(
"DownloadPriceInformations",
"DownloadPriceInformations/{folder}/{filename}",
new
{
controller = "Document",
action = "DownloadPDF"
});
但我很好奇是否可以在没有重载函数的情况下工作并将这种情况映射到 Global.asax 中的RegisterRoutes,以便能够从多个参数中创建一个参数:
routes.MapRoute(
"DownloadPriceInformations",
"DownloadPriceInformations/{folder}/{filename}",
new
{
controller = "Document",
action = "DownloadPDF",
// How to procede here to have a parameter like 'folder\filename'
filepath = "{folder}\\{filename}"
});
问题变得有点长,但我想确保你得到我想要的结果。
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
谢谢,我不知道这个!
标签: asp.net-mvc asp.net-mvc-routing url-routing