MVC Core 网站开发(Ninesky) 2、栏目

栏目是网站的常用功能,按照惯例栏目分常规栏目,单页栏目,链接栏目三种类型,这次主要做添加栏目控制器和栏目模型两个内容,控制器这里会用到特性路由,模型放入业务逻辑层中(网站计划分数据访问、业务逻辑和Web层,初步计划划分如下图)。

模块功能划分

一、栏目控制器

1、添加控制器

Ninesky.Web项目项目Controller文件夹点右键 添加->新建项

image

在添加新项对话框中选择MVC控制器类,名称输入CategoryController.

image

自动生成代码如下

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Threading.Tasks;
  5 using Microsoft.AspNetCore.Mvc;
  6 
  7 
  8 namespace Ninesky.Web.Controllers
  9 {
 10     /// <summary>
 11     /// 栏目控制器
 12     /// </summary>
 13     public class CategoryController : Controller
 14     {
 15         // GET: /<controller>/
 16         public IActionResult Index()
 17         {
 18             return View();
 19         }
 20     }
 21 }
 22 

 

2、使用特性路由。

改造一下Index方法,使其接受id参数,返回id字符串形式。

  1  public IActionResult Index(int id)
  2         {
  3             return Content(id.ToString());
  4         }

运行效果

image

这里生成的地址index感觉累赘,如果把index去掉显示效果会更好。

为index方法添加特性路由,添加完成代码如下:

  1         // GET: /<controller>/
  2         [Route("/Category/{id:int}")]
  3         public IActionResult Index(int id)
  4         {
  5             return Content(id.ToString());
  6         }

[Route("/Category/{id:int}")]表示路由形式为/catgory/id参数,id参数只接受int类型。F5运行效果如下

image

 

二、添加模型

1、新建Base项目

解决方案(Ninesky)上点右键->添加->新建项目

image

选择.NET Core –> Class Library(.NET Core)

名称输入:Ninesky.Base(这里添加了一个新项目,是考虑到项目分层,Web项目负责显示,地下有业务逻辑和数据存储的层。 我把栏目相关的模型,业务逻辑放到Base项目中。)

 

image

Base项目中将默认生成的Class1改名为Category。

2、为项目添加EntityFrameworkCore包

Ninesky.Base项目的引用中右键-> 管理NuGet程序包

 

image

在对话框中选择浏览标签,搜索框中输入“EntityFrameworkCore”,然后找到”Microsoft.EntityFrameworkCore”点击安装。

image

 

这里安装的是EntityFrameworkCore的1.1版本,需要NETStandard.Library1.6.1版本的支持,而项目自带的是NETStandard.Library1.60版本,所以出现了叹号。

image

再次进入Nuget管理器中巴NETStandard.Library的版本更新一下,叹号立即消失。

image

3、添加栏目类型

栏目类型是个枚举(General,Page,Link)

栏目类型:常规栏目,单页栏目,链接栏目。

常规栏目:可以添加子栏目,设置内容模型后可以添加相应内容。

单页栏目:只一个页面,页面可以设置页面内容。

链接栏目:一个转向链接。

Ninesky.Base项目中右键->添加->。 输入类名”CategoryType

image

将代码修改如下

  1  using System.ComponentModel.DataAnnotations;
  2 
  3 namespace Ninesky.Base
  4 {
  5     /// <summary>
  6     /// 栏目类型
  7     /// </summary>
  8     public enum CategoryType
  9     {
 10         [Display(Name = "常规栏目")]
 11         General,
 12         [Display(Name = "单页栏目")]
 13         Page,
 14         [Display(Name = "链接栏目")]
 15         Link
 16     }
 17 }
 18 

 

4、添加栏目模型

4.1、公共模型类

打开Category.CS,修改代码如下:

  1 using System.ComponentModel.DataAnnotations;
  2 
  3 namespace Ninesky.Base
  4 {
  5     /// <summary>
  6     /// 栏目模型
  7     /// </summary>
  8     public class Category
  9     {
 10         [Key]
 11         public int CategoryId { get; set; }
 12 
 13         /// <summary>
 14         /// 栏目名称
 15         /// </summary>
 16         [Required]
 17         [StringLength(50)]
 18 
 19         public string Name { get; set; }
 20 
 21         /// <summary>
 22         /// 栏目类型
 23         /// </summary>
 24         [Required]
 25         [Display(Name= "栏目类型")]
 26         public CategoryType Type { get; set; }
 27 
 28         /// <summary>
 29         /// 上级栏目ID
 30         /// </summary>
 31         /// <remarks>
 32         /// 0-表示本栏目是根栏目,无上级栏目
 33         /// </remarks>
 34         [Required]
 35         [Display(Name = "上级栏目")]
 36         public int ParentId { get; set; }
 37 
 38         /// <summary>
 39         /// 排序
 40         /// </summary>
 41         /// <remarks>
 42         /// 数字越小越靠前
 43         /// </remarks>
 44         [Required]
 45         [Display(Name = "排序")]
 46         public int Order { get; set; }
 47 
 48         /// <summary>
 49         /// 打开目标
 50         /// </summary>
 51         [Required]
 52         [StringLength(20)]
 53         [Display(Name = "打开目标")]
 54         public string Target { get; set; }
 55 
 56         /// <summary>
 57         /// 栏目说明
 58         /// </summary>
 59         [Required]
 60         [StringLength(1000)]
 61         [Display(Name = "栏目说明")]
 62         public string Description { get; set; }
 63     }
 64 }

4.2添加常规栏目模型

Ninesky.Base项目中右键->添加->。 输入类名”CategoryGeneral”,代码如下:

  1 using System.ComponentModel.DataAnnotations;
  2 
  3 namespace Ninesky.Base
  4 {
  5     /// <summary>
  6     /// 常规栏目模型
  7     /// </summary>
  8     public class CategoryGeneral
  9     {
 10         [Key]
 11         public int GeneralId { get; set; }
 12 
 13         /// <summary>
 14         /// 栏目ID
 15         /// </summary>
 16         [Required]
 17         [Display(Name = "栏目ID")]
 18         public int CategoryId { get; set; }
 19 
 20         /// <summary>
 21         /// 栏目视图
 22         /// </summary>
 23         [Required]
 24         [StringLength(200)]
 25         [Display(Name = "栏目视图")]
 26         public string View { get; set; }
 27 
 28         /// <summary>
 29         /// 模块名称
 30         /// </summary>
 31         [Required]
 32         [StringLength(50)]
 33         [Display(Name = "模块名称")]
 34         public string Module { get; set; }
 35 
 36         /// <summary>
 37         /// 内容视图
 38         /// </summary>
 39         [Required]
 40         [StringLength(200)]
 41         [Display(Name = "内容视图")]
 42         public string ContentView { get; set; }
 43 
 44         /// <summary>
 45         /// 内容排序
 46         /// </summary>
 47         [Required]
 48         [StringLength(200)]
 49         [Display(Name = "内容排序")]
 50         public int? ContentOrder { get; set; }
 51 
 52         /// <summary>
 53         /// 栏目
 54         /// </summary>
 55         public virtual Category Category { get; set; }
 56     }
 57 }
 58 

4.3添加单页栏目模型

Ninesky.Base项目中右键->添加->。 输入类名”CategoryPage”,代码如下:

  1 using System.ComponentModel.DataAnnotations;
  2 
  3 namespace Ninesky.Base
  4 {
  5     public class CategoryPage
  6     {
  7         [Key]
  8         public int PageId { get; set; }
  9 
 10         /// <summary>
 11         /// 栏目ID
 12         /// </summary>
 13         [Required]
 14         [Display(Name = "栏目ID")]
 15         public int CategoryId { get; set; }
 16 
 17         /// <summary>
 18         /// 栏目内容
 19         /// </summary>
 20         [Required]
 21         [StringLength(10000)]
 22         [Display(Name = "栏目内容")]
 23         public string Content { get; set; }
 24 
 25         /// <summary>
 26         /// 栏目视图
 27         /// </summary>
 28         [Required]
 29         [StringLength(200)]
 30         [Display(Name = "栏目视图")]
 31         public string View { get; set; }
 32 
 33         /// <summary>
 34         /// 栏目
 35         /// </summary>
 36         public virtual Category Category { get; set; }
 37 
 38         public CategoryPage()
 39         {
 40             View = "Index";
 41         }
 42     }
 43 }
 44 

4.4添加链接栏目模型

Ninesky.Base项目中右键->添加->。 输入类名”CategoryLink”,代码如下:

  1 using System.ComponentModel.DataAnnotations;
  2 
  3 namespace Ninesky.Base
  4 {
  5     /// <summary>
  6     /// 链接栏目模型
  7     /// </summary>
  8     public class CategoryLink
  9     {
 10         [Key]
 11         public int LinkId { get; set; }
 12 
 13         /// <summary>
 14         /// 栏目ID
 15         /// </summary>
 16         [Required]
 17         [Display(Name = "栏目ID")]
 18         public int CategoryId { get; set; }
 19 
 20         /// <summary>
 21         /// 栏目地址
 22         /// </summary>
 23         [Required]
 24         [DataType(DataType.Url)]
 25         [StringLength(500)]
 26         [Display(Name = "栏目地址")]
 27         public string Url { get; set; }
 28     }
 29 }
 30 

返回到公共模型Category.CS中,在底部添加外键

  1         /// <summary>
  2         /// 栏目说明
  3         /// </summary>
  4         [Required]
  5         [StringLength(1000)]
  6         [Display(Name = "栏目说明")]
  7         public string Description { get; set; }
  8 
  9 //添加的导航属性
 10         /// <summary>
 11         /// 常规栏目
 12         /// </summary>
 13         public virtual CategoryGeneral General { get; set; }
 14 
 15         /// <summary>
 16         /// 单页栏目
 17         /// </summary>
 18         public virtual CategoryPage Page { get; set; }
 19 
 20         /// <summary>
 21         /// 链接栏目
 22         /// </summary>
 23         public virtual CategoryLink Link { get; set; }
 24 //添加的导航属性结束
 25     }
 26 }
 27 

三、其他

代码托管地址:https://git.oschina.net/ninesky/Ninesky

文章发布地址:http://www.ninesky.cn

                    http://mzwhj.cnblogs.com/

代码包下载:http://pan.baidu.com/s/1dFBmg0p

 

返回目录

posted @ 2016-12-04 23:09  洞庭夕照  阅读(2804)  评论(4编辑  收藏  举报