Stephen
Stephen

Reputation: 803

.net MVC Areas and subfolders

I am using Areas in my project but I would like to organise my views within these areas into sub folders.

For example, I have an area called Members which contains a Controller called Settings and another Controller called MemberCategory. MemberCategory is in effect a subfolder of Settings.

I would like when I access the Member Category view for my url to resolve to Members/Settings/MemberCategory

At present it resolves to Members/MemberCategory

Is it possible to nest the views into subfolders and change the controller to point to

return View("Members/Settings/MemberCategory");

Or do this need to done with routing?

Any examples would be appreciated.

Upvotes: 1

Views: 1406

Answers (2)

Frank
Frank

Reputation: 11

You can also do something like:

return this.View("../MailTemplates/ResetPassword");

To get to the view you want to use. The code editor will not be able to resolve this, but it works.

I think this is cleaner than:

return this.View("~/Areas/Cms/MailTemplates/ResetPassword");

Upvotes: 1

Stephen
Stephen

Reputation: 803

I have resolved this problem with Routes and not nesting the views into subfolders.

In my Area Registration file I have added the following above the default route:

    context.MapRoute(
        "MemberCategory",
        "Members/Settings/MemberCategory",
        new { controller = "MemberCategory", action = "Index" }
    );

    context.MapRoute(
        "MemberCategoryAction",
        "Members/Settings/MemberCategory/{action}/{id}",
        new { controller = "MemberCategory", action = "Index", id = UrlParameter.Optional }
    );

Not sure if this is the most elegant way of doing this but it works in my case.

Upvotes: 5

Related Questions