路线和Url助手混淆

时间:2011-08-23 08:21:29

标签: asp.net-mvc-3

由于这个原因,我对MVC前端有点困惑,我定义了以下默认路由;

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

当我使用Url助手时,即

@Url.Action("MyAction")

它会生成此网址;

/?action=MyAction&controller=MyController

并且从未找到我的动作方法。帮助者如何生成Urls以及如何更正?

2 个答案:

答案 0 :(得分:1)

只需使用重载来指定操作和控制器:

@Url.Action("MyAction", "MyController")

如果使用仅采用操作名称的重载,则控制器将从当前路径数据中获取。默认路由不会进入它。

@Url.Action("MyAction")

相当于

@Url.Action("MyAction", (string)ViewContext.RouteData.Values["controller"])

答案 1 :(得分:0)

我有同样的问题,我有一个使用WebForms构建的Web应用程序,并慢慢地将部件迁移到MVC,以支持我有一个路由条目,最终破坏路由评估代码并导致有趣的动作网址

this blog post解决了我的问题

相关问题