1

I have routes:

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

and usage:

@Url.Action("News", "News", new { newsId = "", newsTitle = "" })

I want to have "/News" url, but instead "/News/News" is being generated. Default route is used I guess. So the question is why NewsRoute is skipped?

1
  • looks like 2 optional paratemers forbidden to use Commented Sep 26, 2011 at 15:13

1 Answer 1

1

The solution was to split route with 2 optional parameters and action to two separate actions:

routes.MapRoute(
    "NewsRoute",
    "News", new {
        controller = "News",
        action = "Index"
    }
);

routes.MapRoute(
    "Specific News",
    "News/{id}/{title}",
    new {
        controller = "News",
        action = "News",
        title = UrlParameter.Optional
    }
);
Sign up to request clarification or add additional context in comments.

Comments

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.