0

I'm trying to build the following route:

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

My controller is as follows:

public class FlightsController : Controller
{
  public ActionResult Index(string destination= null, string origin=null)
      {

      // some code here

      }
}

I want to browse to this address--> www.somewebsite.com/guide/austria/flights

and reach my action Index (flight/index), with destination=austria

Is it possible? and how? the method I tried doesn't work :/

5
  • which version of MVC are you using ? Commented Mar 3, 2015 at 12:24
  • it's been a while since I worked with MVC, but it looks like it should work to me. Do you have any conflicting routes mapped? Commented Mar 3, 2015 at 12:24
  • No, only the default route.. @Francis, 4.0.0.0 Commented Mar 3, 2015 at 12:26
  • 1
    Is the default defined before it of after it? Might be worth switching the order and trying... just in case! Commented Mar 3, 2015 at 12:28
  • hehe... yeah u right... i didn't even think about it xD tnx. working. Commented Mar 3, 2015 at 12:30

1 Answer 1

1

I suspect the issue is due to the order in which you have defined the routes. It is important to note that they are evaluated from top to bottom (first defined, to last defined). Once a valid match is found it will stop checking for other matches.

If you have the "default" route defined first, it will cause issues because it generally matches a lot of URL patterns.

So I would suggest you move the "default" to be the last one and that should solve the issue. Always try to define the most restrictive patterns at the top, and work down to the move flexible ones (like "default").

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.