开发者

MVC 3 Route problem

ActionLink result "http://localhost:5089/Article/GetArticlesByCategory?category=ASP.NET&categoryId=2". i want to show that link type "http://localhost:5089/Blog/ASP.NET". what is wrong route named "Article".

Routes:

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 }

Link:

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

SOLVED

GetArticlesByCategory parameter int categoryId changed to >> string catego开发者_开发技巧ry and replaced action codes as to new parameter (string category)

Routes replaced with:

        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 replaced with:

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


There are a few issues. First, and most important, your routes are specified in the wrong order. The default route should be defined last. Second, never define a route with two optional parameters. It just causes too many problems.

Try the following for your routes:

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
);


You are not specifying the action in the route

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

I suggest you use Phil Haack's routes debug, http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx. A great way for debugging your MVC routes


If you want the link to show as http://localhost:5089/Blog/ASP.NET, you'll need to change the actionlink as such:

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

Since you don't want the CategoryID in the link, there is no need to put it in. the route isn't being matched by the actionlink because it expects a CategoryID parameter as well

EDIT

If you want the CategoryID to be read from the route, it needs to be added to the route. otherwise it will just be appended as a parameter (like in your original example).

If you change your route to:

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

The link will now look like Blog/2/ASP.NET or Blog/ASP.NET/2 but if you want the categoryId to be read from the URL, then I don't think you have much choice

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜