3

trying to get started with ASP.NET MVC.

I encountered a few difficulties while setting up basic routes.

My routes are as follows:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        routes.MapRoute(
            name: "ImproItem",
            url: "{controller}/{action}/{type}",
            defaults: new { controller = "ImproItemForm", action = "Index", type = UrlParameter.Optional }
        );
    }

My view calls :

  <li>@Html.ActionLink("linkLabel", "Index", "ImproItemForm", new { type = "blablabla" }, null)</li>

My controller action sig is:

 public class ImproItemFormController : Controller
    {
...
  public ActionResult Index(String t)
        {
...}
}

The view generates the following HTML:

<li><a href="/ImproItemForm?type=blablabla">linkLabel</a></li>

This looks OK to me. However, this link correctly calls the controller's action (using ImproItem route) but it does not pass the blablabla argument. The parameter t = null when I debug the app.

Can you explain me why? What should I change to correctly receive the blablabla argument?

Now if I start the application and try to browse

Also is it normal that when I browse: http://localhost:55193/ImproItemForm/Index?id=foobar It does call the ImproItemFormController.Index(String t) method ? I did not expect that this URL would match with this route:

    routes.MapRoute(
        name: "ImproItem",
        url: "{controller}/{action}/{type}",
        defaults: new { controller = "ImproItemForm", action = "Index", type = UrlParameter.Optional }
    );

I thought the argument needs to have the same name than in the route : type and not id.

Thx in advance for your help.

Anik Saha
  • 4,313
  • 2
  • 26
  • 41
A D
  • 307
  • 3
  • 21
  • `type` is not `t`. Change the method to `ActionResult Index(String Type)` or the link paramater to `new { t = "blablabla" }` (in which case the route would need to be `url: "{controller}/{action}/{t}", –  Jan 19 '16 at 23:16

2 Answers2

2

I thought the argument needs to have the same name than in the route : type and not id.

Actually when you request the URL - http://localhost:55193/ImproItemForm/Index?id=foobar, it actually calls the Default route only, and not the custom route that you have created. Default route has - controller name, action name and id. That means, if there is any URL matching this pattern (i.e., {controllername}/{actionname}/{id}) would match the default route.

Order of routes are very important in route collection because route table is built top-to-bottom, so as soon as the URL finds its first matching route, it stops scanning further.So ideally, default route should be the bottom most route in route collection.

I guess all your problems for this particular scenario should be resolved by performing following two steps-

  1. Move default route to the bottom in route collection in RouteConfig.cs
  2. Rename parameter "t" to "type" in Index action.
Nirman
  • 6,715
  • 19
  • 72
  • 139
  • 4
    I would add a third point: make ImproItem route specific to its controller. Currently, it would catch other controllers calls, defeating the default route purpose once your first point is done. Url of ImproItem should be changed to: `url: "ImproItemForm/{action}/{type}"` – Frédéric Jan 19 '16 at 18:00
  • Thanks Nirman and Frédéric for your assistance, this is now very clear to me :) – A D Jan 20 '16 at 09:53
1

Change the name of the parameter in your action to match the name in the ActionLink

public ActionResult Index(String type)
Electric Sheep
  • 3,867
  • 1
  • 29
  • 38