ASP.NET MVC 4 Routing Query - Passing query-string to Index Action

12,290

Update It's worth pointing out, /ControllerName/Index/4 should work with the default route.

With the default route there, it expects the second parameter to be the controller name.

So with the Default Route /ControllerName/4 is being interpereted as ControllerNameController Action 4, which of course does not exist.

If you add

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

before the default one it would allow

/Home/4 to be routed to HomeController action Index with id=4

I have't tested this, it may conflict with the default. You may need to specify the controller explicitly in the route, ie:

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

(Obviously, replace Home with whatever controller you're actually wanting to route to)

Share:
12,290
Gravy
Author by

Gravy

Updated on June 05, 2022

Comments

  • Gravy
    Gravy almost 2 years

    I have a controller with an index action.

    public ActionResult Index(int id = 0)
    {
    
        return view();
    }
    

    I wish to pass id into the index action, however it doesnt appear to work in the same way as the details action.

    e.g. if I want to pass id 4 into the index action, I have to visit url:

    http://localhost:8765/ControllerName/?id=4
    

    With the details Action... I can do this.

    http://localhost:8765/ControllerName/Details/4
    

    What I want to do with Index is something like...

    http://localhost:8765/ControllerName/4
    

    When I visit this url, I get an error:

    Server Error in '/' Application.
    
    The resource cannot be found.
    
    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 
    
    Requested URL: /fix/1
    
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
    

    Is this possible? How can I get MVC to automatically treat the index action in the same way as the details one?

    Thanks

    UPDATE - MY CURRENT ROUTES CONFIG

    public class RouteConfig
    {
        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 }
            );
        }
    }
    

    UPDATE NEW RouteConfig Class still doesn't work when I visit localhost:1234/Fix/3

    public class RouteConfig
    {
        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: "FixIndexWithParam",
                url: "Fix/{id}",
                defaults: new { controller = "Fix", action = "Index", id = UrlParameter.Optional });
        }
    }
    
  • Gravy
    Gravy over 11 years
    thanks, I created a new route, but still problems... see updated question.
  • Gravy
    Gravy over 11 years
    sorry, I responded before i pasted it in. Please check update
  • Glenn Slaven
    Glenn Slaven over 11 years
    You need to add the new route before the default route, they're executed in order
  • Erik Funkenbusch
    Erik Funkenbusch over 11 years
    You can also add a route constraint, so that it will only match if the parameter is an integer.
  • Gravy
    Gravy over 11 years
    How do I attach a route constraint?
  • Glenn Slaven
    Glenn Slaven over 11 years