0

I have a folder crated in my application called admin inside the controllers, so my folder structure looks like this

-Models
|
-Views
|
-Controller
  |
  |-HomeController.cs
  |
  |-Admin
     |
     |-HomeController.cs <-- in this controller, i have methods Add,Delete,View

i needed to created a route so if i type the url http://localhost:2336/admin/add, it will execute the add method of the home controller, but i get a 404 error.

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

            routes.MapRoute(
            name: "Admin",
            url: "Admin/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "fms.Controllers.Admin" }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
0

1 Answer 1

1

Your admin route specifies a route like this:

Admin/{controller}/{action}/{id}

This means in your URL you need to have the controller name as well as the action. For example, this should work:

http://localhost:2336/admin/home/add

If you change the url route to this it will work:

Admin/{action}/{id}

However, this does mean it's a little pointless to have your admin controller in it's own folder. If I were you I'd look up how to create a new MVC area.

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

3 Comments

I am getting this error Multiple types were found that match the controller named 'Home'.
You probably need to specify a namespace for your other route then. Or use an area which makes all this go away :)
i already did fms.Controllers.Admin or are you talking about the default routes

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.