Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Two controllers, same name and mapping, different areas #83

Closed
sthewissen opened this issue Aug 28, 2014 · 4 comments
Closed

Two controllers, same name and mapping, different areas #83

sthewissen opened this issue Aug 28, 2014 · 4 comments

Comments

@sthewissen
Copy link

I have two productscontrollers, one for an admin area and one for outside of it. The admin area one is in namespace Project.Admin.Controllers and the normal one is in Project.Controllers. My routes are configured like this:

MapDelete = true;
map.Resource<Controllers.ProductsController>(p => { p.Only("index"); p.As("products"); });
map.Area("admin", admin =>
{
    admin.Resources<Controllers.Admin.ProductsController>(n => { n.As("products"); });
}

The resulting error I get when running the application is:

the controller 'products' only has methods Show, Update, New, Edit, Destroy, Create, Delete.

Exception Details: RestfulRouting.Exceptions.InvalidRestfulMethodException: the controller 'products' only has methods Show, Update, New, Edit, Destroy, Create, Delete.`

When removing the p.Only("index") statement, the app runs, but when I look at the RouteDebug I see that there is no Index route for the normal ProductsController, which is basically the only action that is actually implemented in there. The Admin version has all actions implemented. Why is no Index action route being generated in this case?

@khalidabuhakmeh
Copy link
Collaborator

At first glance, your area code doesn't look right. When you define an area, you need to also give it a type, so that it can scope that namespace.

 map.Area<AreasController>("mappings", area => {
        area.Resource<ResourceController>();
        area.Resources<ResourcesController>(resources => {
            // we are nesting a resource inside of another resource
            resources.Resources<OtherResourcesController>(other => other.Only("index"));
        });
        area.Resource<AreasController>();
        area.Resource<ExtrasController>();
    });

What you are trying to do I've done recently, so it is possible. I would recommend making an AdminRouteSet and connecting it to your default one.

@khalidabuhakmeh
Copy link
Collaborator

After posting my previous comment I realized your issue might be in your view / url generation. Be sure to include the area when generating your urls.

@Url.Action("index", "products", new { area = "admin" })

The "area" parameter is only necessary when you are generating the area url from outside the area (I hope that makes sense). As a gotcha, when you want to leave an area, be sure to set the area to an empty parameter.

@Url.Action("index", "products", new { area = "" })

Routes are a really strange beast with nuance that is easily missed. Took me years to figure them out, hopefully I can shorten that time for you :).

@sthewissen
Copy link
Author

I've put a sample with this issue and issue #82 on the following repo: https://github.com/dvolvenl/RestfulRoutingNotHappening

What you can do to test this issue is:

  • Run the sample and click the two buttons for admin and normal Products.
  • The normal one should not work, the admin should.
  • In Routes.cs I've added two statements. The first one is commented out, but uncommenting it crashes the app.
  • In RouteDebug you can see that there's no Index route being generated for the normal Product resource.

@sthewissen
Copy link
Author

Was caused by using .Resource instead of .Resources.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants