You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PartialView中@Html.ActionLink无法匹配控制器DownloadAnexo动作报404

问题原因及解决方法

1. 控制器名参数传递错误

ASP.NET MVC的Html.ActionLink/Url.Action辅助方法中,控制器名参数不需要带Controller后缀,你传入NoticiasController会导致路由匹配时查找名为NoticiasControllerController的控制器,自然触发404错误。
修改方法:把第三个参数的NoticiasController改为Noticias即可,示例:

// 修正后的ActionLink写法
@Html.ActionLink("Download", "DownloadAnexo", "Noticias", new { noticiaid = Model.ID, filename = f.File }, null)

// 修正后的Url.Action写法
<a href="@Url.Action("DownloadAnexo", "Noticias", new { noticiaid = Model.ID, filename = f.File })">
 Download
</a>

如果修改控制器名后仍然出现404,检查你的RouteConfig配置,确认默认路由规则没有被修改,默认规则下参数没有匹配到路由占位符时会自动作为查询字符串传递,你的参数传递方式本身没有问题。

2. 控制器下载逻辑存在错误(修正路由后仍会报错的问题)

你当前的DownloadAnexo方法里读取文件的路径只拼接到了文件夹层级,没有加上文件名,读取文件时会抛出系统异常:

// 错误写法,fullPath是文件夹路径,不能直接作为ReadAllBytes的参数
string fullPath = ConfigurationManager.AppSettings["NoticiasFiles"] + "/N_" + noticiaid + "/Anexos/";
byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);

修正后的逻辑需要把文件名拼接到完整路径中:

public ActionResult DownloadAnexo(int? noticiaid, string filename)
{
    if(noticiaid != null && !String.IsNullOrEmpty(filename))
    {
        // 拼接完整的文件路径
        string dirPath = ConfigurationManager.AppSettings["NoticiasFiles"] + "/N_" + noticiaid + "/Anexos/";
        string fullPath = Path.Combine(dirPath, filename);
        byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, filename);
    }
    else
        return Content("参数无效");
}

额外说明

PartialView没有不能直接向控制器传参的限制,你遇到的问题和PartialView无关,属于路由匹配和代码逻辑错误导致的。

内容的提问来源于stack exchange,提问作者DanielCaps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 23:57:03