3

I'm making asp.net mvc application and I have next issue. For example I need produce url like this www.something.com/abc where abc is product ID and www.something.com/def where def is company ID.

Can someone show me some part of code with route link like this?

@Html.RouteLink("Sample link 1", "routeName 1", 
     new {controller = "Home", action = "action name 1", parameter="abc" })

@Html.RouteLink("Sample link 2", "routeName 2", 
     new {controller = "Home", action = "action name 2", parameter="def" })

Just to clarify more my question, for example:

this is routing system

  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "aaaaa",
                url: "{id}",
                defaults: new { controller = "Home2", action = "Index2" }
            );
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "bbbb",
                url: "{id}",
                defaults: new { controller = "Home3", action = "Index2" }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

And these are routelinks

@Html.RouteLink("bbbb", "aaaaa",new { id = 555 })
@Html.RouteLink("bbbb", "bbbb", new { id = 6666666, controller="Home3"})

And both of them are redirecting me to same action controller home2 and action Index2.

But I specified which route to use "aaaaa" for first and "bbbb" for secound

And I also specified different controller in second one.

VirtuoZ
  • 163
  • 2
  • 5
  • 14
  • How does the routing engine differentiate a product from a company without a controller? Doesn't it make the url confusing? – Mathieu Guindon Jun 30 '13 at 15:43
  • if `www.something.com/abc` is a product, and `www.something.com/def` is a company, what is `www.something.com/ghi`? Is it product or company? – Matt Houser Jun 30 '13 at 18:57
  • I want to call routes by name from html.routelink – VirtuoZ Jul 01 '13 at 06:24
  • VirtuoZ, this is an identical question which you asked and I replied to you like Darin Dimitrov. http://stackoverflow.com/questions/17388972/map-route-asp-net-mvc/17389685#comment25267780_17389685 – Huske Jul 03 '13 at 09:41

1 Answers1

3

You cannot have 2 identically looking urls:

be routed to 2 different controller actions. The routing engine has absolutely no way of disambiguating between them. When a request of this form comes in, the routing engine evaluates your routes in the order they are defined and it matches this one:

routes.MapRoute(
    name: "aaaaa",
    url: "{id}",
    defaults: new { controller = "Home2", action = "Index2" }
);

That's the reason why Home2 controller is executed. You should distinguish between the notion of generating an url (with the Html.RouteLink helper) where you have the possibility of specifying the route name and evaluating a route.

If you want to be able to disambiguate between those 2 urls you will need to use constraints. For example:

routes.MapRoute(
    name: "aaaaa",
    url: "{id}",
    defaults: new { controller = "Home2", action = "Index2" },
    constraints: new { id = @"\d{1,3}" }
);

routes.MapRoute(
    name: "bbbb",
    url: "{id}",
    defaults: new { controller = "Home3", action = "Index2" },
    constraints: new { id = @"\d{4,10}" }
);

In this example the first route accepts ids with 1 to 3 digits whereas the second route accepts ids with 4 to 10 digits.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • But I define route in @Html.RouteLink("bbbb", "bbbb", new { id = 6666666, controller="Home3"}), "bbbb" is route? – VirtuoZ Jul 01 '13 at 08:54
  • Yeah, you define route in Html.RouteLink. That's all. When the user clicks on this link the browser sends a GET request to your application. That's where the routing engine, given an url of the form `http://example.com/555`, should decide which controller action to invoke. As you can see there's no longer the route name in this url. So how do you think the routing engine will know that it should invoke `Home3`? Let me tell you how: it can't, unless you specify constraints as explained in my answer. If you don't specify constraints, the routing engine simply picks what's most logic: the first. – Darin Dimitrov Jul 01 '13 at 08:55
  • So what is solution, or is there solution for that, main reason is to hide controller and action for SEO optimization?? – VirtuoZ Jul 01 '13 at 08:59
  • Use a single route: `aaaa`. It should be enough. As you can understand you cannot hide vital information from the url (the action). The only possible workaround is to have some constraints or simply leave the action in your url. – Darin Dimitrov Jul 01 '13 at 09:02
  • But how when i type URL www.example.com I accssess controller and action and they don't appear in URL ? – VirtuoZ Jul 01 '13 at 09:45
  • Because you have the `Default` route. But you can have only one such route. With only one default controller and only one default action. – Darin Dimitrov Jul 01 '13 at 10:50
  • @VirtuoZ, Darin replied to you in exactly the same way as I did in http://stackoverflow.com/questions/17388972/map-route-asp-net-mvc/17389685#comment25267780_17389685. No offence or anything, but you need to get the hang of routes and to understand ASP.NET Routing system. – Huske Jul 03 '13 at 09:43