0

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

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's normal. The 2 routes are mutually exclusive. You will need to define some constraints. For example if your ids are always numeric you could define a numeric regex constraint on the first route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.