【问题标题】:The resource cannot be found ( controller action MVC 5, .NET 4.6.1)找不到资源(控制器操作 MVC 5、.NET 4.6.1)
【发布时间】:2016-04-09 09:08:48
【问题描述】:

我试图下载一个文件,当我点击一个超链接时,我总是得到一个 Resource not found 异常。

控制器动作永远不会被击中。

索引.cs.html:

@{
    ViewBag.Title = "Files";
}

@Html.ActionLink("Download Report", "DownloadFile", "FileDownload", new { type = "pdf" }, new { @class = "" })

渲染的动作链接: http://localhost:58255/FileDownload/DownloadFile?type=pdf

当我在 routeConfig 中取消注释 MapRoute 条目“文件”时,操作链接呈现如下: http://localhost:58255/FileDownload/DownloadFile/pdf

但它们都不起作用! -> 找不到资源

控制器(视图加载时只有索引操作被点击,但当我点击操作链接时什么都没有):

[AllowAnonymous]
public class FileDownloadController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

FilePathResult DownloadFile(string type)
{
   var fn = $"~/Documents/Test.{type}";
   return File(fn, "application/pdf", Server.UrlEncode(fn));
}

RegisterRoutes.cs:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes(); // for attribute routing on action
AreaRegistration.RegisterAllAreas();

//routes.MapRoute(
//    name: "Files",
//    url: "{controller}/{action}/{type}",
//    defaults: new { controller = "FileDownload", action = "DownloadFile", type = UrlParameter.Optional }
//);

// Should be last
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

...来自 WebForms 区域...

【问题讨论】:

    标签: asp.net-mvc-routing asp.net-mvc-5.2 html.actionlink


    【解决方案1】:

    请尝试在操作名称前添加 public 访问修饰符

    public FilePathResult DownloadFile(string type)
    {
      var fn = $"~/Documents/Test.{type}";
      return File(fn, "application/pdf", Server.UrlEncode(fn));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 2011-10-01
      • 2014-11-29
      相关资源
      最近更新 更多