我正在尝试设置路由控制器,以便在浏览器在应用启动时缓存特定文件时获取特定文件的请求。我可以在控制台中看到请求:
应用程序缓存进度事件(472的275)http://mywebsite.com/Path/Whatever/App/Views/SubFolder/Reports/SomeProposal.html
我设置了Global.asax
来调用RouteConfig.RegisterRoutes
(虽然只是OOTB代码):
protected void Application_Start()
{
using (StreamWriter sw = System.IO.File.AppendText(@"C:\Users\tory.waterman\Desktop\app.txt"))
{
sw.WriteLine("app start fired at " + DateTime.Now.ToString());
}
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
我已经确认这实际上正在运行并注册我的路线(我写入带有时间戳的文本文件):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Reports",
url: "Path/Whatever/App/Views/SubFolder/Reports/{report}",
defaults: new
{
controller = "Reports",
action = "GetReport"
}
);
}
(路径的某些位明显改变了)
我有一个名为ReportsController
的控制器,我希望调用GetReport()
方法:
public ActionResult GetReport(string report)
{
return View();
}
但它永远不会被调用(我写入Global.asax
和RouteConfig
这样的文本文件,但它永远不会发生。)
任何想法如何让我的工作或我做错了什么?
编辑1:向string report
添加了GetReport
参数。仍然是同一个问题。
答案 0 :(得分:2)
假设您的路线定义为
routes.MapRoute(
name: "Reports",
url: "Path/Whatever/App/Views/SubFolder/Reports/{report}", //with route param
defaults: new
{
controller = "Reports",
action = "GetReport"
}
);
您的操作方法签名需要report
参数,否则路线不匹配:
public ActionResult GetReport(string report)
{
return View();
}