0

asp.net mvc routing pattern is

 {"some_parameter/{controller}/{action}/{id}"}

Is this a valid format if some_parameter can be null or string empty

2
  • Updated the order of the routes so it would work correctly. It will not work if you place this route before the default route. Commented Dec 20, 2011 at 12:24
  • @Nario, could you provide more insight into what you want to achieve with this? Just curious. Because this is additional complextity and you will have to deal with constraints anyway. Commented Dec 20, 2011 at 13:17

2 Answers 2

3

I believe that what you wanted is {some_parameter}/{controller}/{action}/{id} (notice curly brackets around "some_parameter") and in that case it shouldn't be null or empty, I think. How do you think your end URL might look like to match the route in case when some_parameter is empty? "mysite.com//mycontroller/myaction/myid"?

Routing engine just matches patterns. If you want to handle both {some_parameter}/{controller}/{action}/{id} and {controller}/{action}/{id}, just define both routes.

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

5 Comments

Creating the two routes like that wont work because that first parameter has no default. Had the same solution but deleted it after you mentioned that.
I thought the same thing. He needs regular expression to match the pattern if you are speedy with those.
@Nario I really don't think this will work if you create routes in that order because you will never hit the second one (the default route)
@Craig, yeah, sure, you need constraints because otherwise (since we have id as optional) in many cases there might be ambiguous URLs like mysite.com/hello/crazy/world/ which would match both routes and will take the first defined by default. Even ordering is no help here as you might have 2 very similar URLs and want them to match different routes.
@VasilioRuzanni ordering will help if his intentions are to match the first route if some_parameter is missing. By the question asked I figured that was their possible intention but more information in the question may provide a better answer.
3

Edit

I've just reordered the route registration so that it would work:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new {
        controller = "home",
        action = "index",
        id = UrlParameter.Optional }
);

routes.MapRoute(
    "Default", // Route name
    "{some_parameter}/{controller}/{action}/{id}", // URL with parameters
    new {
        some_parameter = UrlParameter.Optional,
        controller = "home",
        action = "index",
        id = UrlParameter.Optional
    }
);

They should be registered in that order. Additionally the second route requires an id and some_parameter parameter otherwise it will never be hit because of the route before it. Even though the some_parameter and id parameters are set to optional, that would never happen because the route before would catch it if it was empty.

3 Comments

some_parameter not have default value. it is dynamic data for example for controller "home" , but for some_parameter I must set empty.
Ordering doesn't help here. You might have 2 URLs that should match different routes. Only constraints could solve that (if even could).
@VasilioRuzanni I believe it does. The constraint he has with this solution as I explained is that the second route must have some_parameter and id set.

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.