danyolgiax
danyolgiax

Reputation: 13086

MVC 3 url parameters rendering

I've this routing map:

routes.MapRoute(
            "ViewNews",
            "{controller}/{action}/{id}/{title}",
            new { controller = "Home", action = "NewsDetail", id = "", title = "" }
        );

and this ActionLink in my View:

@Html.ActionLink(Model.Title, "NewsDetail", new { id = Model.Id, title = Url.ToFriendlyUrl(Model.Title) })

I expect it renders something like this:

http://localhost:49327/Home/NewsDetail/1/news-title

instead it renders:

http://localhost:49327/Home/NewsDetail/1?title=news-title

what I miss?

Update

I've moved MapRoute before default in this way:

routes.MapRoute(
        "ViewNews",
        "{controller}/{action}/{id}/{title}",
        new { controller = "Home", action = "NewsDetail", id = "", title = "" }
        );


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

        );

but now when I request the initial URL:

http://localhost:49327/Home

I get instantly redirected to:

http://localhost:49327/Home/NewsDetail/1/news-title

"NewsDetail" has become the default action!

enter image description here

Upvotes: 0

Views: 302

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

Make sure that the Model.Id parameter is not null or empty and that the Default route is not before the one you have shown in your Global.asax. You need to remove it or place it after this custom route.

Upvotes: 1

Related Questions