1

希望在阅读 MVC 路由后得到一些帮助,但自己没有想出答案。

我注册了以下路线:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            null,
            "YourFeedback/Article/{resourceId}",
            new { controller = "YourFeedback", action = "Index", contentTypeId = new Guid(ConfigurationManager.AppSettings["ArticleLibraryId"]) });

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

我在 aspx 视图中有以下 ActionLink:

<%=Html.ActionLink("Your Feedback", "Article", "YourFeedback", new
 {
     resourceId = Model.ContentId.ResourceId
 }, new { @class = "yourFeedback" })%>

我对 MVC 路由的理解是,这将呈现一个带有“/YourFeedback/Article/101”href 的锚链接,其中 101 来自 Model.ContentId.ResourceId。

然而,锚链接 href 呈现为“YourFeedback/Article/resourceId=101”。

有什么想法我哪里出错了吗?

提前致谢。

4

1 回答 1

2

这是因为您的操作链接将匹配第二条路线而不是第一条路线。原因是您的第一条路线中有一些奇怪的默认值。您已将控制器设置为“YourFeedback”并将操作设置为“索引”。这意味着如果您想匹配该路线,您也必须在您的操作链接中设置它。

要匹配路线,您必须使用此操作链接:

<%=Html.ActionLink("Your Feedback", "Index", "YourFeedback", new
{
    resourceId = Model.ContentId.ResourceId
}, new { @class = "yourFeedback" })%>

或者改变路线。

于 2010-06-16T10:00:18.097 回答