3

simple question. I want something like:

http:/ /www.mywebsite.com/microsoft or http:/ /www .mywebsite.com/apple so microsoft and apple should be like id but i use it just like controller in the default

this is the default route

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

This produce something like http:/ /www.mywebsite .com/home/aboutus or http: //www.mywebsite .com/products/detail/10

I added another route

routes.MapRoute(
            "Partner", // Route name
            "{id}", // URL with parameters
            new { controller = "Home", action = "Partners"}, // Parameter defaults
            new { id = @"\d+" }
        );

but this has constraint that only allow numeric id.

how do I accomplish what I wanted.

thanks

3 Answers 3

4

If the expression can contain only letters and digits you could modify the constraint:

routes.MapRoute(
    "Partner", // Route name
    "{id}", // URL with parameters
    new { controller = "Home", action = "Partners"}, // Parameter defaults
    new { id = @"^[a-zA-Z0-9]+$" }
);
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure exactly what you are trying to achieve but it looks like you need a custom route constraint. Take a look here for an example:

http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Custom-route-constraint-to-validate-against-a-list.aspx

Remember to register the route constraint first

Comments

0

If you don't want to provide a numeric constraint, just delete the 4th parameter, ie

routes.MapRoute("Partner", "{id}", new { controller = "Home", action = "Partners"});

The 4th parameter is an anonymous object that provides constraints for the route parameters that you have defined. The names of the anonymous object members correspond to the route parameters - in this case "controller" or "action" or "id", and the values of these members are regular expressions that constrain the values that the parameters must have in order to match the route. "\d+" means that the id value must consist of one or more digits (only).

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.