MVC 3路由问题

时间:2011-09-27 01:13:25

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing

ActionLink结果“http:// localhost:5089 / Article / GetArticlesByCategory?category = ASP.NET& categoryId = 2”。我想显示链接类型“http:// localhost:5089 / Blog / ASP.NET”。什么是错误的路线命名为“文章”。

路线:

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

 routes.MapRoute(
                "Article",
                "Blog/{category}", // 
                new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional, categoryId = UrlParameter.Optional }

链接:

@Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name, categoryId = k.CategoryId }, null)

解决

GetArticlesByCategory参数int categoryId更改为>>关于新参数(字符串类别)的字符串类别和替换的操作代码

路线替换为:

        routes.MapRoute(
            "Category",
            "Blog/{category}",
            new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional }
        );

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

ActionLink替换为:

@Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
new { category = k.Name }, null)

3 个答案:

答案 0 :(得分:2)

有一些问题。首先,最重要的是,您的路线以错误的顺序指定。应该最后定义默认路由。其次,永远不要定义具有两个可选参数的路线。这只会导致太多问题。

请尝试以下路线:

routes.MapRoute(
    "CategoryAndId",
    "Blog/{category}/{categoryId}", 
    new { controller = "Article", action = "GetArticlesByCategory" } 
);

routes.MapRoute(
    "CategoryOnly",
    "Blog/{category}", 
    new { controller = "Article", action = "GetArticlesByCategory",
        category = UrlParameter.Optional } 
);

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

答案 1 :(得分:1)

您没有在路线中指定行动

routes.MapRoute(
                "Article",
                "Blog/{action}/{category}/{categoryId}", // 
                new { controller = "Article", action = "GetArticlesByCategory", category = UrlParameter.Optional, categoryId = UrlParameter.Optional }

我建议您使用Phil Haack的路线调试http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx。调试MVC路由的好方法

答案 2 :(得分:0)

如果您希望链接显示为http://localhost:5089/Blog/ASP.NET,则需要更改动作链接:

@Html.ActionLink(k.Name, "GetArticlesByCategory", "Article",
    new { category = k.Name }, new { @title = "Kategorisindeki Makaleler", @class = "selected" })

由于您不想在链接中使用CategoryID,因此无需将其放入。该路由未与actionlink匹配,因为它还需要CategoryID参数

修改

如果要从路径中读取CategoryID,则需要将其添加到路径中。否则它将作为参数附加(如在原始示例中)。

如果您将路线更改为:

"Blog/{categoryId}/{category}"
or
"Blog/{category}/{categoryId}"

此链接现在看起来像Blog/2/ASP.NETBlog/ASP.NET/2但是如果您希望从网址中读取categoryId,那么我认为您没有太多选择