用户可以下载位于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"
});
但我很好奇是否可以在没有重载函数的情况下工作并将这种情况映射到RegisterRoutes
Global.asax 中,以便能够从多个参数中创建一个参数:
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}"
});
问题变得更长了,但我想确保你得到我想要的结果。