MVC控制器没有触发

时间:2014-11-14 17:19:31

标签: c# asp.net-mvc routes

我正在尝试设置路由控制器,以便在浏览器在应用启动时缓存特定文件时获取特定文件的请求。我可以在控制台中看到请求:

  

应用程序缓存进度事件(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.asaxRouteConfig这样的文本文件,但它永远不会发生。)

任何想法如何让我的工作或我做错了什么?

编辑1:向string report添加了GetReport参数。仍然是同一个问题。

1 个答案:

答案 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();
}