2

I am trying to get a URL for the following route:

RouteTable.Routes.MapRoute(
    null,
    "cinema",
    new
{
    Controller = "CinemaListings",
    Action = "ShowCinemaLandingPage",
    SiteArea = "CinemaTimes",
    MainLandingPage = true
});

I've tried the following methods to produce the URL:

<%:Url.Action("ShowCinemaLandingPage", "CinemaTimes", new { SiteArea = "CinemaTimes", MainLandingPage = true})%>
<%:Url.RouteUrl(new { Controller = "CinemaTimes", Action = "ShowCinemaLandingPage", SiteArea = "CinemaTimes", MainLandingPage = true })%>    

I've also tried with only the controller and action names. I get null returned - what am I missing?

1
  • What 'type' is SiteArea? Is it a string parameter? Can you show us the controller/action with parameters that this maps to? Commented Jul 14, 2011 at 13:21

2 Answers 2

3

Doesn't this work?

<%:Url.Action("ShowCinemaLandingPage", "CinemaListings", new { SiteArea = "CinemaTimes", MainLandingPage = true })%>

It seems from your code that you've mixed up SiteArea with Controller values. So instead of providing controller name in Url.Action, you've provided SiteArea twice (as second parameter and as additional route value).

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

1 Comment

Argh! I'll give that a test a get back to you walks away kicking himself
-1

It is hard to understand your route. First of all, you have not given it name (passing null which I have experienced before causing problem) and then there is no place holder in there.

This cannot work without giving it a controller.

<%:Url.Action("ShowCinemaLandingPage", "CinemaTimes", new { SiteArea = "CinemaTimes", MainLandingPage = true})%>

Here you are passing controller name but since there is no controller name, no path will be matched.

So I would use - at bare minimum - this:

RouteTable.Routes.MapRoute(
    "GiveMeSomeNameWillYa?",
    "{Controller}/{Action}",
    new
{
    Controller = "CinemaListings",
    Action = "ShowCinemaLandingPage",
    SiteArea = "CinemaTimes",
    MainLandingPage = true
});

2 Comments

The route does work for incoming requests. Any requests to /cinema will be handled by the ShowCinemaLandingPage on the CinemaListings controller. I need to maintain this behaviour. I've tried adding the route name but this had no effect.
-1 Route name is completely optional. The URL template doesn't require controller and action tokens, if these are provided with default values.

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.