0

I'm just learning MVC and am trying to create a SEO page title, therefore I'm sending an ID for the controller but also the page slug for the SEO.

I want to see Recipe/Details/18/foobar

but the below code returns

Results/Name/18?name=foobar

I presume its an issue with my custom Route but from all mt Research I think I'm doing it correctly. Any assistance is appreciated.

_PartialView

<a href="@Url.Action("Details", "Recipe", new {id = Recipe.ID, name = Recipe.Slug}, null)">

RouteConfig

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Recipe",
            url: "{controller}/{action}/{id}/{name}",
            defaults: new {
                            controller = "Recipe",
                            action = "Details",
                            id = UrlParameter.Optional,
                            name = UrlParameter.Optional
                            }
        );

Controller

public ActionResult Details(int id)

1 Answer 1

1

You should add your custom routes before the default one. They are processed in order, so in your case the mvc uses the default route.

Regards, Mihail

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

2 Comments

My only issue is now my Default route no longer fires when I load my project without specifying Home/Index in the url ?
I finally fixed it by adding a constraint to my new Route. again, thx for your help.

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.