nopcommerce是国外asp.net领域一个高质量的b2c开源项目,基于EntityFramework和MVC开发,QQ群1:75272942(2000人超级群,已满) QQ群2:640322459

导航 - 搜索

教你一招 - nopcommerce如何启用redis缓存?

<RedisCaching Enabled="true" ConnectionString="localhost:6379,allowAdmin=true" />
3、刷新页面就可以了,在Redis Desktop Manager里面看下缓存数据吧。



Autofac一般是通过Contrller的构造函数或者属性来注入,但是这有一个共同点就是调用这个类的方法一般都是实例方法,也就是要实例化这个类才能调用它的方法。但是如果它是一个静态方法我们又该怎么办呢?其实也很简单,下面我们就通过一个写日志的组件来介绍怎么实现ASP.NET MVC5类的静态方法的依赖注入。因为考虑到很多地方都要用到写日志这个方法,而且我们又不想每次调用都需要new一个对象,所以把调用方法封装成一些静态方法。


  1. using Autofac;
  2. using Autofac.Integration.Mvc;
  3. using Lanhu.Services;
  4. using Lanhu.Services.MetionNowOrder;
  5. using Lanhu.Services.Member;
  6. using System.Web.Mvc;
  7. namespace Lanhu.Admin
  8. {
  9. public class DependencyRegistrar
  10. {
  11. public static void RegisterDependencies()
  12. {
  13. var builder = new ContainerBuilder();
  14. builder.RegisterControllers(typeof(MvcApplication).Assembly);
  15. builder.RegisterType<Log_UserLogonService>().As<ILog_UserLogon>().InstancePerLifetimeScope();
  16. builder.RegisterType<Log_UserOperateService>().As<ILog_UserOperate>().InstancePerLifetimeScope();
  17. //autofac 注册依赖
  18. IContainer container = builder.Build();
  19. DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  20.     }
  21.  }
  22. }


  1. using Lanhu.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using System.Web.Optimization;
  8. using System.Web.Routing;
  9. namespace Lanhu.Admin
  10. {
  11. public class MvcApplication : System.Web.HttpApplication
  12. {
  13. protected void Application_Start()
  14. {
  15. AreaRegistration.RegisterAllAreas();
  16. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  17. RouteConfig.RegisterRoutes(RouteTable.Routes);
  18. BundleConfig.RegisterBundles(BundleTable.Bundles);
  19. DependencyRegistrar.RegisterDependencies();
  20. }
  21. }
  22. }


  1. using Lanhu.Model;
  2. using Lanhu.Services;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. namespace Lanhu.Admin.Infrastructure
  11. {
  12. public class LogFacade
  13. {
  14. public static bool AddLogonLog(MLog_UserLogon model)
  15. {
  16. var service = DependencyResolver.Current.GetService<ILog_UserLogon>();
  17. return service.Insert(model);
  18. }
  19. public static bool AddOperateLog(string info,bool isError=false)
  20. {
  21. var service = DependencyResolver.Current.GetService<ILog_UserOperate>();
  22. if (isError)
  23. model.ErrorMsg = info;
  24. else
  25. model.Msg = info;
  26. return service.Insert(model);
  27. }
  28. public static bool AddOperateLog(MLog_UserOperate model)
  29. {
  30. var service = DependencyResolver.Current.GetService<ILog_UserOperate>();
  31. return service.Insert(model);
  32. }
  33. }
  34. }


教你一招 - 如何在nopCommerce里面使用 Entity Framework (EF) Code-First Migrations(英)

I've seen a lot of nopCommerce forums users asking how they can use Entity Framework (EF) Code-First Migrations to customize nopCommerce, add new fields and entites to the core. I actually use a lot of EF Migrations myself when doing nopCommerce customization projects, and I must say it helps a lot in the development.

Today, I'll share with you how you can do that in nopCommerce project! I'll be using nopCommerce 3.20 as an example, but you can easily apply the concept to other vesions!

Setting Up EF Migrations in nopCommerce Solution

The first thing you want to do is to enable migrations in your nopCommerce solution. So fire up nopCommerce in Visual Studio, look at Nop.Web project, and open Web.config. You need to add a connection string to your development database. Note that adding the connection string in Web.config doesn't affect how nopCommerce works, since nopCommerce doesn't look for connection string in Web.config.

EF Migrations in nopCommerce - Setup Web.config

Then, open NopObjectContext.cs in Nop.Data. Add a new constructor that points to the name of the connection string that you've just added in previous step. NOTE: replace Pro-nopCommerce with the name of your connection string.

EF Migrations in nopCommerce - Setup NopObjectContext.cs

The next step is to actually enable migrations in the project. If you have not already done so, bring up Package Manager Console. In the "Default Project" drop-down list, select Nop.Data as the project. Please also make sure Nop.Web is selected as the StartUp Project. Now, enter the command "enable-migrations" in Package Manager Console, and hit Enter! Visual Studio will generate a file named "Configurations.cs"; you can safely ignore it, but you need to keep it.

EF Migrations in nopCommerce - Enable migrations

The last step in EF Migrations setup is done by entering the command "add-migration InitialMigration -IgnoreChanges" in Package Manager Console. "InitialMigration" (highligted yellow) is the name you want to give the the current migration, and the "IgnoreChanges" handle is to tell EF Migrations that you want to leave out the current database as-is: that means you want EF to ignore all the existing tables so that no script is generated for the existing tables.

EF Migrations in nopCommerce - Initial Migration

As a result, you'll see a new .cs file generated by Visual Studio that correspond to the migration - InitialMigration - you've just added. If you look at the file, it essentially is a blank file, due to the fact that we used the -IgnoreChanges handle in previous step.

To actually save this migration over to the database, run the command "update-database" in Package Manager Console. Your database is now ready for actual EF Migration tasks!

EF Migrations in nopCommerce - Update Database

Adding a Migration and Updating the Database

Now, suppose we want to link up Blog and Products, where a Product can have multiple BlogPost that talk about the Product itself. We'll need a one-to-many relationship between Product and BlogPost.

In essence, what we need is a new ProductId field in BlogPost, and a ICollection<BlogPost> property in Product. The following screenshot sums up the update we should add to both Product.cs and BlogPost.cs (in Nop.Core project).

EF Migrations in nopCommerce - BlogPost.cs

EF Migrations in nopCommerce - Product.cs

Now you have the properties ready on your domain model, you also need to setup the configuration and tell Entity Framework how you want to format the parameter, for example the relationship, and whether a field is optional/required. Open BlogPostMap.cs in Nop.Data, and enter the following lines, where we tell EF to that Product (or ProductId) is an optional property (meaning it can have NULL as value), and a Product can have many BlogPost, and to use ProductId as the foreign key between BlogPost andProduct.

EF Migrations in nopCommerce - BlogPostMap

We now have enough information to instruct Entity Framework to generate the migration, so again, bring up Package Manager Console, and enter the command "add-migration AddProductToBlogPost".

EF Migrations in nopCommerce - Add Product To Blog Post

This command causes Visual Studio to generate a .cs file that looks like the following:

EF Migrations in nopCommerce - Generated Migration CS file

To actually generate the SQL script and update the database, run again the command "update-database" in Package Manager Console. Now, to verify that the database is updated correctly, open up BlogPost table in the database, and check that the new field, foreign key and etc are indeed added.

EF Migrations in nopCommerce - Verifying Database


Entity Framework Code-first Migration can be a very handy tool if you customize nopCommerce a lot. Like this you don't need to touch the database when you want to add new fields, tables and etc to the database. Everything can be done from the code!

Learn up the technique, and have fun coding!


nopcommerce 添加新表流程

nop_demoService.cs和nop_demoService.cs 。



 需要在文件Presentation\Nop.Admin\Infrastructure\AutoMapperStartupTask.cs 中添加相应的映射
Mapper.CreateMap<Nop_demo , Nop_demoModel>();
Mapper.CreateMap<Nop_demoModel, Nop_demo>();


教你一招 - Misc类型插件的妙用(附带插件源码)


例如我们进入后台促销管理 - 折扣管理,你会看到这个页面:





源码下载 (170.9KB)




1. 如何汉化
首先在官网下载汉化包 (3.1KB)
解压后放到目录\Presentation\Nop.Web\Content\tinymce\langs,然后找到\Presentation\Nop.Web\Administration\Views\Shared\EditorTemplates\RichEditor.cshtml,这个view就是初始化tinymce的地方,在tinymce.init里面添加配置信息language: 'zh_CN',然后保存刷新页面就看到中文了。

2. 如何支持本地图片上传
找到文件\Presentation\Nop.Web\Administration\Views\Shared\EditorTemplates\RichEditor.cshtml,把其中的canUploadPictures = false;改为canUploadPictures = true; 保存刷新页面即可。




- 拥有一个强密码来保护我们的,因为我们每天都在网上共享个人信息
- 拥有一个强密码来保护我们,如信用卡信息,社会保障,银行帐号,密码#S等敏感信息

- 一个强大的密码至少有8个字符
- 一个强大的密码应该有特殊字符
- 一个强大的密码应该有字母和数字
- 一个强大的密码应该有小写字母和大写字母

- 请在至少2个月更改一次密码的做法。
- 避免使用正确的拼写。例如:你应该使用“ef4t”代替单词“effort”。


1) 在nopCommerce代码,注册页面在以下位置:
根目录\Views\Customer\ Register.cshtml   <-- 打开这个文件

2) 在代码中找到这里:
<div class="form-fields">
                    <div class="inputs">
                        @Html.LabelFor(model => model.Password, new { }, ":")
                        @Html.EditorFor(model => model.Password)
                        @Html.RequiredHint(sp;                     @Html.EditorFor(model => model.Password)@Html.Esp;@H3.= zstclass="form-fields">
                    ant;">@Html.EditorFor(model => model.Password)@Html.Esp;@H3.= zst"inputs">
                   1nbsp>@H3.= zst"inputs">
(软Dx; fonCaptchale="color: #000000;">@Html.EditorFor(model => model.Password)@Html.Esp;@H3.= zst"inputs"&iv style="background-color: #ffffff;">                   1nbsp>@H3.= zst            &nLabelFor(model => model.Password, new { }, ":")
                  &nbsspebsp;nd-color: #f8f8f8;">    &84nbsp;     class="form-fields">
 &nbsle="background-color: #f8f8f8;">                "color: blue;">"inputs"}006699; font-weight: bold;">class="form-fields">"inputs"
26. ”.net/aue:14px; 762570dden > / 教你一招 / { }, "inputs"() &iv style="background-color: #ffffff;"> &nbsle="background-color: #f8f8f8;">    "color: blue;">"inputs"// noneapplie33); fStinymce">This; foplug-is="Apyour p ular ele息l006699; font-weight: bold;">class="form-fields""inputs"})div style="background-color: #ffffff;"> &nbsle="background-color: #f8f8f8;">    "color: blue;">"inputs"// Fixuppyour tyle="coile icator (inserts it explicitly af/spayour p de sty ele息l#006699; font-weight: bold;">class="form-fields""inputs" &nbsle="background-color: #f8f8f8;">"color: blue;">"inputs" &nbs#000000;">= / > / 教你一招 / new { }, new { }, js他bSansx="0commerce中tiStiiv; fProgra查s辑Sl; orFiles_Extras/js/jquery.dx">js_CN'ode style="color: blue;">":" / new { }, new { }, js他bSansx="0commerce中tiStiiv; fProgra查s辑Sl; orFiles_Extras/js/stinymce>js_CN'ode style="color: blue;">":"
stinymce>js >如n>们将 >
/ 教你一招 / { }, "inputs" &nbsle="background-color: #f8f8f8;">    "color: blue;">"inputs"         "color: blue;">"inputs" &nbsle="backgroun / wcf / g"> erce3.3>w.nopcnopComm/a>
PubDate">26. ;&w.nopch:p;w.nopchiroun 20pxdecof="htt: de>er/a> ;"ILES%2f2014%2f07%2ftinymce_languagwww.nopchina.net6.nePubDate"Pag ad2PubDate"Pag 0px 0px 10www.nopchif="http://www.nopchina.net6.nep de stytinymce地ges/%a0%e4%b8%80w.nopcnopComm/a>
P.S.p;&w.nopch此> 过测试.tin了检查仪3.30.Mistml" class="tagged注:p;&w.nopch度检查仪佂