< back

Configure StoreBuilder with new ASP.NET MVC Project

Step 1 - Create a new MVC Project

Step 2 - Add StoreBuilder Nuget Packages

StoreBuilder hosts a private nuget server for our components. If this is your first time using StoreBuilder Nuget, please see Configuring Visual Studio to access the StoreBuilder Nuget Server

Add StoreBuilder Nuget Packages:

  • StoreBuilder
  • StoreBuilder.AspNetIdentity
  • StoreBuilder.HandlebarsCompiler
  • StoreBuilder.Mvc
  • StoreBuilder.WebsiteManager

Optionally:

  • StoreBuilder.Log4net
  • StoreBuilder.Admin

Note: StoreBuilder requires logging. You do not have to use Log4net but if you do not then you must have some other logging library configured and operational for StoreBuilder to run.

Step 3 - Configure StoreBuilder to use AspNetIdentity security provider for authentication

Add StoreBuilder section to your web project's web.config file.

Part A - Add a new <section> to your <configSection> element:

<section name="storebuilder" type="StoreBuilder.Config, StoreBuilder" />

Part B - Add <storebuilder> configuration element.

<storebuilder>
  <database connectionStringName="StoreBuilder" />
  <logProvider type="StoreBuilder.Log4Net.LogProvider, StoreBuilder.Log4Net" />
  <securityProvider type="StoreBuilder.AspNetIdentity.IdentitySecurityProvider, StoreBuilder.AspNetIdentity" />
  <license accountKey="XXXX-XXXX-XXXX-XXXX" licenseKey="XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX" />
</storebuilder>

EnsurePart yourC logProvider- configuration element hasProvide the correct valuevalues for the typeaccountKey attribute.and Here,licenseKey we show an exampleattributes of the <license> node.

Step 4 - Configure StoreBuilder Logging

Recall that StoreBuilder requires logging to run. This step describes configuring it for the StoreBuilder.Log4net package.Nuget package mentioned in step 2. However, you may use some logging library other than Log4net.

Optional Part CA - addAdd a <logProvider> element to the <storebuilder> configuration element that you added in Step 3 - Part B.

  <logProvider type="StoreBuilder.Log4Net.LogProvider, StoreBuilder.Log4Net" />

Part B - Add the <log4net> configurationelement element Ifto youyour have<configuration> installed StoreBuilder.Log4netnode.

<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value=".\\App_Data\\StoreBuilder.Web.log" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Size" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="MaximumFileSize" value="10MB" />        
      <param name="CountDirection" value="1" />
      <param name="StaticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="LogFileAppender" />
    </root>
</log4net>

Note: Recall that StoreBuilder requires logging. This step is optional if you choose to configure some logging library other than Log4net, but you must configure a logging library for StoreBuilder to run.

Step 45 - Hook in StoreBuilder routing for MVC

Edit the default RouteConfig.cs/App_Start/RouteConfig.cs file generated by the MVC project templatetemplate.

Part byA removing- Remove the default route mapping.

routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

andPart addB - Add the following line as the last route table entry:

routes.Add(newentry StoreBuilderRouteProcessor("{*catchAll}",thereby new MvcRouteHandler()));

This effectively passes onpassing all unhandled routes to StoreBuilder for processing. StoreBuilder will then internally resolve ecommerce related routes as well as handle any 404 not found routes.

routes.Add(new StoreBuilderRouteProcessor("{*catchAll}", new MvcRouteHandler()));

Part C - Add the following using clause at the top of the file.

using StoreBuilder.Mvc.Routing;

Step 56 - Add StoreBuilder namespaces to your Web.config for Razor views

Add the StoreBuilder namespace to the Web.config in your Views folder so that StoreBuilder HtmlHelper extension methods work properly in your Razor views.

<configuration>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="StoreBuilder" />
        <add namespace="StoreBuilder.Models" />
        <add namespace="StoreBuilder.Mvc" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>

Step 6 - Add a PageWrapper razor view

The StoreBuilder.Mvc nuget package contains an MVC StoreBuilderController which is used to integrate StoreBuilder functionality into a regular MVC project. While all StoreBuilder core features and plugins use Handlebars for templating, markup intended to be rendered as a page will be passed through this StoreBuilderController and wrapped with a surrounding Razor view in order to make site wide consistency easy for the MVC developer.

In your Views folder, add a new folder called 'StoreBuilder'. Then add a new Razor view in the StoreBuilder folder named 'PageWrapper.cshtml'.

As a minimum this template may appear as follows:

@model StoreBuilder.Content.Results.PageContentResult
@Html.Raw(Model.Body)
@section styles{
@Html.RenderStyles(Model.ThemeResourceArea)
}
@section scripts{
    @Html.RenderScripts(Model.ThemeResourceArea)
}

For StoreBuilder.WebsiteManager add a new Razor view in the Sharded folder named '_CaptiveLayout.cshtml'

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    @RenderSection("styles", required: false)
</head>
<body itemscope itemtype="http://schema.org/WebPage">
    @RenderBody()
    @RenderSection("scripts", required: false)
</body>
</html>

also add a new Razor view in StoreBuilder folder named 'CaptivePageWrapper.cshtml'.

@*
    This razor view is a wrapper for content rendered by StoreBuilder.
    The intention of this razor view is to wrap "captive" pages that should have no regular site navigation elements.
*@
@model StoreBuilder.Content.Results.PageContentResult
@{
    Layout = "~/Views/Shared/_CaptiveLayout.cshtml";
}
@section styles{
    @Html.RenderStyles(Model.ThemeResourceArea)
}
@Html.Raw(Model.Body)
@section scripts{
    @Html.RenderScripts(Model.ThemeResourceArea)
}

Step 7 - Create a /themes folder for your StoreBuilder themes

StoreBuilder will be default look for a /themes folder in the root of your project to discover any StoreBuilder theme files. Each theme should be contained within a subfolder of the root /themes folder.

The default theme should be aptly named 'default' and contained within the /themes/default folder. While not absolutely required, StoreBuilder recommends a functional organization structure for themes with subfolders for functional areas of the website:

  • /everywhere contains JS/CSS that should be included everywhere on your website (all pages).
  • /listing contains JS/CSS/HBS files that will be loaded/used on product listing type pages in addition to anything that was included 'everywhere'. Listing pages include Category pages, Brand pages, Tag pages and Search results.
  • /product contains JS/CSS/HBS files that will be loaded/used on product pages in addition to anything that was included 'everywhere'.
  • /cart contains JS/CSS/HBS files that will be loaded/used on the cart page in addition to anything that was included 'everywhere'.
  • /checkout contains JS/CSS/HBS files that will be loaded/used on the checkout page(s) in addition to anything that was included 'everywhere'.
  • '/account` contains JS/CSS/HBS files that will be loaded/used on the customer account page(s) in addition to anything that was included 'everywhere'.
  • /plugins contains JS/CSS/HBS files that have been provided by any installed plugins. After a plugin has been installed, you have the option to customize the plugin JS/CSS/HBS files to match your branding or unique requirements.
  • /emails contains HBS templates for emails that StoreBuilder may send.

For more information see StoreBuilder ThemeEngine.

Optional, Use a pre-built StoreBuilder theme to get started quickly

If you want to get started quickly you can add one of our pre-built themes to your project using Nuget.

  • StoreBuilder.Bootstrap3Theme

To use the theme with our pre-built Header, Navigation and Footer update your '_Layout.cshtml' file to be similar to the following

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    @Html.RenderPageMeta()
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    @Html.RenderStyles("everywhere")
    @RenderSection("styles", required: false)
    <link href='//fonts.googleapis.com/css?family=Questrial' rel='stylesheet' type='text/css'>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
    @Html.RenderTemplate("sb-header", new { Environment = StoreBuilderEnvironment.Current, SearchString = Request.QueryString["q"] })
    @Html.RenderTemplate("sb-navbar", new { Environment = StoreBuilderEnvironment.Current })
    @Html.RenderBreadcrumb()
    <div class="container">
        @RenderBody()
    </div>
    @Html.RenderTemplate("sb-footer", new { Environment = StoreBuilderEnvironment.Current })
    @RenderSection("scripts", required: false)
</body>
</html>

Step 8 - Secure the /storebuilder project path

If you installed the StoreBuilder.Admin nuget package, it will have added the StoreBuilder folder to your web project which contains an admin portal for StoreBuilder. All the API used by this admin portal will already be secured, however securing the admin portal files will add one more layer of protection to your website and prevent public review of your admin portal code or configuration.

<location path="storebuilder">
  <system.web>
    <authorization>
      <allow roles="StoreBuilder.Admin" />
      <deny users="*" />
    </authorization>
  </system.web>
</location>