【发布时间】:2021-08-06 09:27:09
【问题描述】:
我正在尝试创建一个操作链接来下载文件。我在视图中获得了 noticiaId 和文件名的值,但在控制器中没有。 我缺少的 actionlink HTML 帮助程序有问题。 如果我这样做:
@Html.ActionLink("Download", "DownloadAnexo", "NoticiasController", new { noticiaid = Model.ID, filename = f.File }, null)
我通过以下 URL 获得 HTTP 404:localhost:port/NoticiasController/DownloadAnexo?noticiaid=55&filename=file.png 这对我来说看起来是正确的,但我仍然收到 404 错误,说找不到路线。
局部视图:
@model Web.Models.Noticias.NoticiaVM
<div class="col">
<label>Anexos:</label>
<table>
@if (Model.Anexos != null)
{
foreach (var f in Model.Anexos)
{
<tr>
<td>@f.File</td>
<td>
@Html.ActionLink("Download", "DownloadAnexo", "NoticiasController", new { noticiaid = Model.ID, filename = f.File }, null)
</td>
</tr>
}
}
</table>
</div>
控制器动作:
public ActionResult DownloadAnexo(int? noticiaid, string filename)
{
if(noticiaid != null && !String.IsNullOrEmpty(filename))
{
string fullPath = ConfigurationManager.AppSettings["NoticiasFiles"] + "/N_" + noticiaid + "/Anexos/";
byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, filename);
}
else
return Content("");
}
我也试过这个:
<a href="@Url.Action("DownloadAnexo", "NoticiasController",
new { noticiaId = Model.ID, filename = f.File })">
Download
</a>
但我得到了相同的结果。 我对局部视图没有太多经验,所以我想知道是否只是因为我将数据从局部直接发送到控制器,而这可能是不可能的。 IDK。
请帮忙。
【问题讨论】:
-
混淆了术语,使您的问题更加混乱。
localhost:port/NoticiasController/DownloadAnexo?noticiaid=55&filename=file.png是 URL,而不是路由,@Html.ActionLink是 HTML 助手,而不是 tag helper。 -
你在
DownloadAnexo上有[HttpPost]吗? -
对术语感到抱歉。我需要在这方面做得更好。谢谢。
-
是的,我有 HttpPost
-
这能回答你的问题吗? ActionLink not calling Controller
标签: c# asp.net .net asp.net-mvc