这是一个Orchard-Modules的入门教程。在这个教程里,我们将开发两个功能页面分别用于数据录入与数据展示。

完成上述简单功能开发,我们一共需要6个步骤。分别为:

上面6个步骤可能不太好理解。在这里,我们把他们转换从MVC中的概念让我们更好理解。

Module

项目模块

Model

实体层

Controller、View

Controller、View

Route

Route

Services

服务层

Admin Menu

后台管理

有点概念后,我们就开始吧!

创建Module

第一步我们需要利用Orchard的代码生成工具Code Generation 来生成Module项目文件。如果对这个命令还不是很熟悉,我们可以在这里先进行了解。

创建:

codegen module XiaoGuang.HelloWorld

使用上述就完成了一个HelloWorld Module的创建。

*关于Module的名称,建议使用系统模块.功能名来命名。

修改清单文件Module.txt:

这个文件用于描述Module信息与依赖关系。因为本次只是一个简单示例,不深入讲解。

Name: XiaoGuang.HelloWorld

AntiForgery: enabled

Author: 互联网新手

Website: http://curd.cnblogs.com

Version: 1.0

OrchardVersion: 1.0

Description: XiaoGuang.HelloWorld演示模块。

Features:

XiaoGuang.HelloWorld:

Description: XiaoGuang.HelloWorld演示模块。

启用:

管理后台->Modules->找到[XiaoGuang.HelloWorld]->点击Enable 或 命令行:feature enable XiaoGuang.HelloWorld

不少朋友开发完Module后,输入了注册路由的地址。发现始终无法看到效果。实际上是Module默认为非启用状态导致。

创建Model

Models目录下新增TestRecord.cs 文件。新增,代码如下:

namespace XiaoGuang.HelloWorld.Models

{

public class TestRecord

{

public virtual int Id { get; set; }

public virtual string Content { get; set; }

}

}

注:如果后续运行提示没有持久化的问题。是因为实体类必须放在命名空间为Models或Records结尾下。

这属于Orchard默认规范,详见:https://orchard.codeplex.com/discussions/267968

codegen datamigration XiaoGuang.HelloWorld

上述语句创建一个实体迁移。并生成如下代码,完成Record对应表创建过程。

public class Migrations : DataMigrationImpl {

public int Create() {

// Creating table TestRecord

SchemaBuilder.CreateTable("TestRecord", table => table

.Column("Id", DbType.Int32, column => column.PrimaryKey().Identity())

.Column("Content", DbType.String)

);

return 1;

}

}

创建Services

新建文件:

ITestService:

public interface ITestService :Orchard.IDependency {

TestRecord GetTest();

TestRecord UpdateTest(string content);

}

TestService:

public class TestService : ITestService {

private readonly IRepository<TestRecord> _testRepository;

public TestService(IRepository<TestRecord> testRepository) {

_testRepository = testRepository;

}

public TestRecord GetTest() {

return _testRepository.Table.FirstOrDefault();

}

public TestRecord UpdateTest(string content) {

var result = GetTest();

if (result == null) {

result = new TestRecord {Content = content};

_testRepository.Create(result);

}

else {

result.Content = content;

_testRepository.Update(result);

}

return result;

}

}

上面的代码的重点是IRepository ,由Orchard封装。实现了实体的增、删、改、查功能。

创建Controller、View

Controller:

public class AdminController : Controller {

public IOrchardServices Services { get; set; }

public ITestService TestService { get; set; }

public AdminController(IOrchardServices services, ITestService testService) {

Services = services;

T = NullLocalizer.Instance;

TestService = testService;

}

public Localizer T { get; set; }

public ActionResult Update(string content) {

TestService.UpdateTest(content);

return RedirectToAction("Index", "Home");

}

}

这里充分体现了依赖注入的好处。只需要构造函数传递接口就可以了。框架自动实例。也易于单测。

View:

@model XiaoGuang.HelloWorld.Models.TestRecord

@{

Layout.Title = T("TestUpdate").ToString();

}

@using (Html.BeginFormAntiForgeryPost(Url.Action("Update", "Admin"))) {

@Html.AntiForgeryToken()

<div class="form-horizontal">

@Html.ValidationSummary(true, "", new {@class = "text-danger"})

<div class="form-group">

@Html.LabelFor(model => model.Content, htmlAttributes: new {@class = "control-label col-md-2"})

<div class="col-md-10">

@Html.EditorFor(model => model.Content, new {htmlAttributes = new {@class = "form-control"}})

@Html.ValidationMessageFor(model => model.Content, "", new {@class = "text-danger"})

</div>

</div>

<div class="form-group">

<div class="col-md-offset-2 col-md-10">

<input type="submit" value="Create" class="btn btn-default"/>

</div>

</div>

</div>

}

创建Route

新增Routes.cs文件。返回指定的路由。

public class Routes :IRouteProvider

{

public IEnumerable<RouteDescriptor> GetRoutes() {

yield return new RouteDescriptor {

Route = new Route("MyHelloWorld", new RouteValueDictionary {

{"area", "XiaoGuang.HelloWorld"},

{"action", "Index"},

{"controller", "Home"}

}, new RouteValueDictionary(), new RouteValueDictionary {

{"area", "XiaoGuang.HelloWorld"}

}, new MvcRouteHandler())

};

yield return new RouteDescriptor

{

Route = new Route("admin/XiaoGuang.HelloWorld/Update", new RouteValueDictionary {

{"area", "XiaoGuang.HelloWorld"},

{"action", "Update"},

{"controller", "Admin"}

}, new RouteValueDictionary(), new RouteValueDictionary {

{"area", "XiaoGuang.HelloWorld"}

}, new MvcRouteHandler())

};

}

public void GetRoutes(ICollection<RouteDescriptor> routes) {

foreach (var route in GetRoutes()) {

routes.Add(route);

}

}

}

创建Admin Menu

该功能用于产生一个后台导航菜单,定位到管理页面。相信代码直接读就可以理解。需要继承于INavigationProvider。

public class AdminMenu : INavigationProvider {

public string MenuName => "admin";

public Localizer T { get; set; }

public void GetNavigation(NavigationBuilder builder) {

builder.AddImageSet("helloworld").Add(T("HelloWorld"), "5", item => {

item.Action("Index", "Admin", new {area = "XiaoGuang.HelloWorld"});

});

}

}

需要特殊说明一下。public string MenuName => "admin";

这段代码是固定值,注意指的大小写。具体原因搜索下INavigationProvider相关引用就知道了。我可是不只一次入坑了。

Orchard入门:如何创建一个完整Module的更多相关文章

  1. CXF 入门:创建一个基于WS-Security标准的安全验证(CXF回调函数使用,)

    http://jyao.iteye.com/blog/1346547 注意:以下客户端调用代码中获取服务端ws实例,都是通过CXF 入门: 远程接口调用方式实现 直入正题! 以下是服务端配置 ==== ...

  2. SpringMVC基础入门,创建一个HelloWorld程序

    ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...

  3. 3dmax实例教程-使用3ds Max 创建一个完整的场景

    本篇教程讲述了利用3ds max创建一个完整的场景. 灵感来源:当我在遇到一些事情睡不着觉的时候我便在努力想象一些别的事情,于是我便想到了这个场景,其实对于我的这个角色我即没有参考图也没有草稿图,有的 ...

  4. SQL Server Database 维护计划创建一个完整的备份策略

     SQL Server维护计划Maintenance Plan这是一个非常有用的维护工具,能够完成大部分的数据库维护任务,通过这些功能包.您可以省略大量的编码时间. 介绍的不是非常多,特此补上一篇 ...

  5. 【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之三

    原文:Getting Started with Sencha Touch 2: Build a Weather Utility App (Part 3) 作者:Lee BoonstraLee is a ...

  6. Angular2快速入门-2.创建一个新闻列表

    背景: 我们想通过一个例子,展示下Angular2 怎么绑定页面,怎么创建Component, 例子:我们创建一个新闻列表,当点击新闻列表中某一条新闻的时候,展示出该条新闻的详细信息, 在详细信息中可 ...

  7. WCF入门, 到创建一个简单的WCF应用程序

    什么是WCF?  WCF, 英文全称(windows Communication Foundation) , 即为windows通讯平台. windows想到这里大家都知道了 , WCF也正是由微软公 ...

  8. 简单创建一个完整的struts2框架小程序

    要完成一个struts2框架的搭建, 1.首先应该从官网上下载最新的jar包,网络连接:http://struts.apache.org/download.cgi#struts2514.1,选择下载F ...

  9. CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用)

    CXF拦截器使用,创建一个使用SOAPHeader的安全验证xml格式: <soap:Header> <auth:authentication xmlns:auth="ht ...

随机推荐

  1. 获取tomcat下路径

    import java.io.File; public class MainTest { public static void main(String[] args) { //获取是项目的绝对路径 S ...

  2. spring-listener&spring-task注解版本

    1.spring-listener: a) import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextLi ...

  3. 如何运用CSS写小三角

    <html> <div class="con"></div> </html> <style> .con{width:0; ...

  4. 把CentOS 7.x网卡名称eno16777736改为eth0

    CentOS 7.x系统中网卡命名规则被重新定义,可能会是"eno167777xx"等,下面我们把网卡名称改为eth0这种. 一.cd  /etc/sysconfig/networ ...

  5. Hibernate 继承映射

    @Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn()public class Animal ...

  6. FAQ

    1.Baudrare and the speed of Byte. 2. Linux FS and Flash store. 3. SW's Coupling. 4. Protocol and Pro ...

  7. this 的值到底是什么?

    你可能遇到过这样的 JS 面试题: var obj = { foo: function(){ console.log(this) } } var bar = obj.foo obj.foo() // ...

  8. eclipse加载maven工程提示pom.xml无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3解决方案

    pom文件提示信息: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 from http:/ ...

  9. MVC架构 使用FastReport

    1.Web.config文件 添加配置 <httpHandlers> <add path="FastReport.Export.axd" verb="* ...

  10. ASP.NET MVC: Razor中的@:和语法

    本文将讨论新版Razor里视图引擎中支持的两个有用的语法功能:@:和<text>语法. 用Razor实现流畅编程 ASP.NET MVC 3配有一个新的名为“Razor”的视图引擎选项(除 ...