New MVC 3 area registration

Posted: March 31, 2012 at 3:05 AM Quote #66142
I'm attempting to follow the same architecture as the administration area (Nop.Admin) to create my own area specific to my website but it doesn't work. I know my AreaRegistration class is being called because the url I mapped works after the application has started but during run-time my default view is trying to use the Nop.Web.Controllers.HomeController controller instead of the one I've specified which is Backoffice.Controllers.HomeController. Is there something special to nopCommerce that I need to do to get my area to register properly? This is nopCommerce 2.3.

Here's my registration class:

namespace Backoffice
{
    public class BackofficeRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get { return "Backoffice"; }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Backoffice_default",
                "Backoffice/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional, area = AreaName },
                new[] { "Backoffice.Controllers" }
            );
        }
    }
}
This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: March 31, 2012 at 4:16 AM Quote #66143
Ok, hunted this down finally. I needed to add my custom area to these files so that nopcommerce knows to look for my area in a specific folder:

Nop.Web.Framework\Themes\ThemableRazorViewEngine.cs
Nop.Web.Framework\Themes\ThemeableVirtualPathProviderViewEngine.cs

Big thanks to RouteDebugger for helping me visually see what routes were matching.
http://haacked.com/archive/2011/04/13/routedebugger-2.aspx

I still have another issue where http://mysite/backoffice throws a 403.14 in IIS and i have to call it like this http://mysite/backoffice/home in order for anything to pop up. Hunting down this issue next..
This post/answer is useful
3
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: March 31, 2012 at 5:12 AM Quote #66144
All kinds of caveats when trying to do a custom area. The last remaining issue was due to the fact that my route url "Backoffice" was the same as the physical folder on the drive. So when browsing http://mysite/backoffice IIS thought I wanted to browse the folder contents and that was causing the 403.14 errors. I renamed my folder to something else, updated the various hard-coded view engine files and now everything works just fine.
This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: March 31, 2012 at 12:37 PM Quote #66156
I am going to follow your post but I think MVC dropped the ball on this one.  Currently, I am building a site that the customer wanted Content Management for, a shopping cart component, and a Blog section.  After trying to get the three applications to "play" together including areas, I finally resorted to creating 3 sites www.mysite.com, donate.mysite.com, and blogs.mysite.com.

IMO, creating a area in order to incorporate another website should be as simple as creating a folder and placing your web app in there.  I ran out of time trying to get areas to work.  I'll have to try your approach.  There are advantages to my approach.  The disadvantages are: needing 3 static ips and possibly 3 site certificates, Skinning 3 different sites to look like one another, creating a universal login for all three.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: March 31, 2012 at 5:15 PM Quote #66171
You can actually just right click the Nop.Web project and choose Add -> Area. This will create an named isolated area for you under a special Area folder and this should work out-of-box with nopCommerce according to the code in their custom view engine (haven’t verified this myself yet). However doing it this way the code is not in a separate .net project/assembly which is what I wanted to continue the existing architecture already present (Nop.Admin) and also have the option of re-usability and easier updates to the code base.

For you current setup you can assign the Host Headers/Names under “Bindings” in IIS7.5 for each of the three sites and still only have one IP. I don’t know squat about certificates; yet. Stackoverflow.com is probably the best place to ask/find out more.

To paint a better picture of doing it as a separate project this was the basic folder setup: (note: your physical folder cannot be the same as your url route or you'll get 403.14 errors)
Nop.Web
  Administration (Nop.Admin project files are under here)
  App_Data
  bin
  Content
  Controllers
  CustomerBackoffice (Backoffice project files are under here)
    Views
      Shared
        _Layout.cshtml (make sure you change code in this file to match the physical folder name)
      _ViewStart.cshtml (make sure you change code in this file to match the physical folder name)
  ...



ThemeableVirtualPathProviderViewEngine.cs


//little hack to get nop's admin area to be in /Administration/ instead of /Nop/Admin/ or Areas/Admin/
if (!string.IsNullOrEmpty(areaName) && areaName.Equals("admin", StringComparison.InvariantCultureIgnoreCase))
{
  var newLocations = areaLocations.ToList();
  newLocations.Insert(0, "~/Administration/Views/{1}/{0}.cshtml");
  newLocations.Insert(0, "~/Administration/Views/{1}/{0}.vbhtml");
  newLocations.Insert(0, "~/Administration/Views/Shared/{0}.cshtml");
  newLocations.Insert(0, "~/Administration/Views/Shared/{0}.vbhtml");
  areaLocations = newLocations.ToArray();
}
if (!string.IsNullOrEmpty(areaName) && areaName.Equals("backoffice", StringComparison.InvariantCultureIgnoreCase))
{
  var newLocations = areaLocations.ToList();
  newLocations.Insert(0, "~/CustomerBackoffice/Views/{1}/{0}.cshtml");
  newLocations.Insert(0, "~/CustomerBackoffice/Views/{1}/{0}.vbhtml");
  newLocations.Insert(0, "~/CustomerBackoffice/Views/Shared/{0}.cshtml");
  newLocations.Insert(0, "~/CustomerBackoffice/Views/Shared/{0}.vbhtml");
  areaLocations = newLocations.Concat(areaLocations).ToArray();
}


ThemableRazorViewEngine.cs

ViewLocationFormats = new[]
              {
                //themes
                "~/Themes/{2}/Views/{1}/{0}.cshtml",
                "~/Themes/{2}/Views/{1}/{0}.vbhtml",
                "~/Themes/{2}/Views/Shared/{0}.cshtml",
                "~/Themes/{2}/Views/Shared/{0}.vbhtml",

                //default
                "~/Views/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.vbhtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Views/Shared/{0}.vbhtml",


                //Admin
                "~/Administration/Views/{1}/{0}.cshtml",
                "~/Administration/Views/{1}/{0}.vbhtml",
                "~/Administration/Views/Shared/{0}.cshtml",
                "~/Administration/Views/Shared/{0}.vbhtml",

                //CustomerBackoffice
                "~/CustomerBackoffice/Views/{1}/{0}.cshtml",
                "~/CustomerBackoffice/Views/{1}/{0}.vbhtml",
                "~/CustomerBackoffice/Views/Shared/{0}.cshtml",
                "~/CustomerBackoffice/Views/Shared/{0}.vbhtml"

              };

PartialViewLocationFormats = new[]
                 {
                   //themes
                  "~/Themes/{2}/Views/{1}/{0}.cshtml",
                  "~/Themes/{2}/Views/{1}/{0}.vbhtml",
                  "~/Themes/{2}/Views/Shared/{0}.cshtml",
                  "~/Themes/{2}/Views/Shared/{0}.vbhtml",

                  //default
                  "~/Views/{1}/{0}.cshtml",
                  "~/Views/{1}/{0}.vbhtml",
                  "~/Views/Shared/{0}.cshtml",
                  "~/Views/Shared/{0}.vbhtml",

                  //Admin
                  "~/Administration/Views/{1}/{0}.cshtml",
                  "~/Administration/Views/{1}/{0}.vbhtml",
                  "~/Administration/Views/Shared/{0}.cshtml",
                  "~/Administration/Views/Shared/{0}.vbhtml",

                  //CustomerBackoffice
                  "~/CustomerBackoffice/Views/{1}/{0}.cshtml",
                  "~/CustomerBackoffice/Views/{1}/{0}.vbhtml",
                  "~/CustomerBackoffice/Views/Shared/{0}.cshtml",
                  "~/CustomerBackoffice/Views/Shared/{0}.vbhtml"

                 };
This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.
E-COMMERCE CONFERENCE 2016
Learn more