找不到资源(控制器操作 MVC 5、.NET 4.6.1)
The resource cannot be found ( controller action MVC 5, .NET 4.6.1)
我正在尝试下载一个文件,当我点击一个 hyperlink 时,我总是得到一个找不到资源的异常。
控制器动作永远不会被击中。
Index.cs.html:
@{
ViewBag.Title = "Files";
}
@Html.ActionLink("Download Report", "DownloadFile", "FileDownload", new { type = "pdf" }, new { @class = "" })
渲染动作-Link:
http://localhost:58255/FileDownload/DownloadFile?type=pdf
当我在 routeConfig 中取消注释 MapRoute 条目 "Files" 时,操作 link 呈现如下:
http://localhost:58255/FileDownload/DownloadFile/pdf
但是它们都不起作用! -> 找不到资源
控制器(当加载视图时只有 Index 操作被触发,但当我点击操作时什么也没有link):
[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 区域...
请尝试在操作名称前添加public访问修饰符
public FilePathResult DownloadFile(string type)
{
var fn = $"~/Documents/Test.{type}";
return File(fn, "application/pdf", Server.UrlEncode(fn));
}
我正在尝试下载一个文件,当我点击一个 hyperlink 时,我总是得到一个找不到资源的异常。
控制器动作永远不会被击中。
Index.cs.html:
@{
ViewBag.Title = "Files";
}
@Html.ActionLink("Download Report", "DownloadFile", "FileDownload", new { type = "pdf" }, new { @class = "" })
渲染动作-Link: http://localhost:58255/FileDownload/DownloadFile?type=pdf
当我在 routeConfig 中取消注释 MapRoute 条目 "Files" 时,操作 link 呈现如下: http://localhost:58255/FileDownload/DownloadFile/pdf
但是它们都不起作用! -> 找不到资源
控制器(当加载视图时只有 Index 操作被触发,但当我点击操作时什么也没有link):
[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 区域...
请尝试在操作名称前添加public访问修饰符
public FilePathResult DownloadFile(string type)
{
var fn = $"~/Documents/Test.{type}";
return File(fn, "application/pdf", Server.UrlEncode(fn));
}