2

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 });
    }
}
3
  • we need to see whats in your RoutesConfig.cs file Commented Oct 7, 2012 at 22:55
  • Question updated with my RouteConfig Commented Oct 7, 2012 at 22:57
  • 1
    FYI: I edited your question to remove the references to EntityFramework, this problem has nothing to do with EF, it's all about the .NET routing implementation and the way it maps those requests to MVC controllers Commented Oct 7, 2012 at 23:21

1 Answer 1

5

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)

Sign up to request clarification or add additional context in comments.

6 Comments

thanks, I created a new route, but still problems... see updated question.
sorry, I responded before i pasted it in. Please check update
You need to add the new route before the default route, they're executed in order
You can also add a route constraint, so that it will only match if the parameter is an integer.
How do I attach a route constraint?
|

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.