Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using asp.net mvc 4 . I want to work with 3 - 4 route pattern , but I can't . it works just with one pattern : this is my RouteConfig file :

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.Add(
        new Route("mycontroller/{action}/{mlid}/{countryid}/{cityid}",
            new RouteValueDictionary(
                new
                { action="myaction",
                    mlid = UrlParameter.Optional,
                    countryid = UrlParameter.Optional,
                    cityid = UrlParameter.Optional,
                }),
                new HyphenatedRouteHandler())
        );
        routes.Add(
        new Route("{controller}/{action}/{id}",
            new RouteValueDictionary(
                new { controller = "start", action = "Index",id = UrlParameter.Optional}),
                new HyphenatedRouteHandler())
        );

I have a controller an action with 8 parameters . I want to use first pattern , but it use second pattern . I used Route Debugger but it does not help me . please help .

EDIT :

I use this code for navigation :

<a href="@Url.Action("myaction", "mycontroller", new { mlid = "1" })">test</a>

but it shows this in address bar :

http://localhost:12911/mycontroller/myaction?mlid=1

I want to show it this :

http://localhost:12911/mycontroller/myaction/1

EDIT :

This is my class :

public class HyphenatedRouteHandler : MvcRouteHandler
        {
            protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                if (requestContext.RouteData.DataTokens["area"] != null)
                {
                    requestContext.RouteData.DataTokens["area"] = requestContext.RouteData.Values["area"].ToString().Replace('-', '_');
                }
                requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");
                requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");
                return base.GetHttpHandler(requestContext);
            }
        }
share|improve this question
    
What do you want your routes to look like? –  edhedges Jun 11 '13 at 13:24
    
@edhedges : I edit it . –  Persian. Jun 11 '13 at 13:34

3 Answers 3

up vote 1 down vote accepted

I would use a routing debugger plugin to track it down. Then it's much easier to see what happens to the routes.

Scott Hanselman suggests Glimpse on this blog post of his:

http://www.hanselman.com/blog/NuGetPackageOfTheWeek5DebuggingASPNETMVCApplicationsWithGlimpse.aspx

I tried to replicate your solution and created the following routes in my project:

        routes.Add(
            new Route("office/{action}/{mlid}/{countryid}/{cityid}",
            new RouteValueDictionary(
                new
                {
                    controller = "office",
                    action = "index",
                    mlid = UrlParameter.Optional,
                    countryid = UrlParameter.Optional,
                    cityid = UrlParameter.Optional,
                }),
                new HyphenatedRouteHandler()
            )
        );
        routes.Add(
            new Route("{controller}/{action}/{id}",
                new RouteValueDictionary(
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
                    new HyphenatedRouteHandler()
                    )
        );

The route can also be added using MapRoute but then you have to write in the following way to be able to use the specific route handler.

        routes.MapRoute(
            name: "RegReq",
            url: "office/{action}/{mlid}",
            defaults: new
            {
                controller = "office",
                action = "index",
                mlid = UrlParameter.Optional
            }).RouteHandler = new HyphenatedRouteHandler();

I then created an office controller with the optional mlid parameter as in this example:

    public ActionResult Index(int? mlid)
    {
        return View();
    }

I never created the view but added a break point here instead to see that mlid really got the id I was sending.

I then added the following link on the home controller's index view:

    <a href="@Url.Action("index", "office", new { mlid = "3" })">test</a>

And when I browse over the test link I get the following URL:

http://localhost:52788/office/index/3

Hopefully this can help you somewhere...

share|improve this answer
    
I used Route Debugger . but i didn't help me . everything is okay on Route Debugger but on browsing has issue . –  Persian. Jun 11 '13 at 13:51
    
Ok. I would try to add controller="mycontroller" to the RouteValueDictionary under the first route. There doesn't seem to be any controller set there so the routing engine might get confused by that. –  Ohlin Jun 11 '13 at 13:55
    
it does not work ! –  Persian. Jun 11 '13 at 14:17
    
Very strange! I created a test project in MVC4 (with my line added above) and then the links were created just in the way you wanted them. I'll update my suggestion (in a few seconds) with the code I was using... –  Ohlin Jun 11 '13 at 14:38
    
Ok . thank you . I'm waiting for you –  Persian. Jun 11 '13 at 14:43

First route does not specify controller at all. The Routing takes the second route and treats 'mlid' as overflow parameter, that is: route value not specified in route definition. Try this:

<a href="@Url.Action("myaction", "mycontroller", new { id = 1 })">test</a>

It uses second route again, but this time parameter name matches to what is specified in the route. You can include as many parameters as you want, just add them to the route, i.e.

{controller}/{action}/{id}/{countryname}

With this, you'll have 'countryname' parameter in controller's RouteData and/or action parameter.

To use first route, assuming you want to use controller named 'mycontroller', try adding it as a default controller like this:

routes.MapRoute(name: "routeName",
url: "mycontroller/{action}/{id}/{countryid}",
defaults: new { controller = "mycontroller",... }...
share|improve this answer
    
this works ! but when I want to do this : <a href="@Url.Action("myaction", "mycontroller", new { id = 1 , countryid = "2"})">test</a> , how it will be ? –  Persian. Jun 11 '13 at 13:48
    
it allways uses second route . i dont want this ! I want to use this link via first route . –  Persian. Jun 11 '13 at 13:49
    
you mean i use just one route ? –  Persian. Jun 11 '13 at 13:56
    
Yes, you used only second, default route, while generating link with Url.Action you got http://localhost:12911/mycontroller/myaction?mlid=1. It is link to controller 'mycontroller', with action 'myaction' and since the route does not specify mlid, but id parameter, you got it as a query string, instead of pretty /1 –  Piotr Cierpich Jun 11 '13 at 14:38
    
I have 8 parameters . It should be a like like : http://localhost:12911/mycontroller/myaction?mlid=1&p1=1&p2=1&p3=1... . and in this way partials does not works .. –  Persian. Jun 11 '13 at 14:45

I think what Piotr was trying to say was that you need to modify this code to add a default controller so that this:

routes.Add(
    new Route("mycontroller/{action}/{mlid}/{countryid}/{cityid}",
        new RouteValueDictionary(
            new
            {
                action="myaction",
                mlid = UrlParameter.Optional,
                countryid = UrlParameter.Optional,
                cityid = UrlParameter.Optional,
            }),
            new HyphenatedRouteHandler())
    );

Becomes this:

routes.Add(
    new Route("mycontroller/{action}/{mlid}/{countryid}/{cityid}",
        new RouteValueDictionary(
            new
            {
                controller="mycontroller",
                action="myaction",
                mlid = UrlParameter.Optional,
                countryid = UrlParameter.Optional,
                cityid = UrlParameter.Optional,
            }),
            new HyphenatedRouteHandler())
    );

Though if you're going to require 10 parameters that are all optional, this may not be the best solution; It's gonna look damn ugly either way.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.