MVC Core website development (Ninesky) 2. Topic, mvcninesky
A topic is a common function of a website. It is divided into regular columns, single-page columns, and link columns by Convention. This time, we mainly add the topic controller and topic model, the Controller will use the feature routing here, and the model will be placed in the business logic layer (the website plan is divided into data access, business logic, and Web layer, as shown in the preliminary plan ).
I. Topic controller 1. Add a controller
InNinesky. WebProjectControllerRight-click a folderAdd->New item
In the Add new project dialog box, selectMVC controller class, Name inputCategoryController.
The automatically generated code is as follows:
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 // topic Controller 12 /// </summary> 13 public class CategoryController: Controller 14 {15 // GET: /<controller>/16 public IActionResult Index () 17 {18 return View (); 19} 20} 21} 22
2. Use feature routing.
Modify the Index method to accept the id parameter and return the id string form.
1 public IActionResult Index(int id) 2 { 3 return Content(id.ToString()); 4 }
Running Effect
The address index generated here is cumbersome. It will be better if the index is removed.
Add a feature route for the index method. The code for adding a feature route is as follows:
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}")] indicates that the Route format is/catgory/id. The id parameter only accepts the int type. The Running Effect of F5 is as follows:
Ii. Add Model 1. Create a Base project
InSolution (Ninesky)Right-clickAdd->Create a project
Select.NET Core->Class Library (. NET Core)
Name input:Ninesky. Base(A new project is added here, which is based on the project hierarchy. The Web project is responsible for displaying the layer of business logic and data storage. I put the topic-related model and business logic in the Base project .)
InBaseTheClass1RenamedCategory.
2. Add a project
EntityFrameworkCore package
InNinesky. BaseProjectReferenceRight-clickManage NuGet packages.
In the dialog box, selectBrowseTag, enter"EntityFrameworkCore", And then find"Microsoft. EntityFrameworkCoreClick Install.
Here, the EntityFrameworkCore version 1.1 is installed and must be supported by NETStandard. Library1.6.1. The project comes with NETStandard. Library1.60, so an exclamation point is displayed.
Log on to the Nuget manager and update the CMB NETStandard. Library version. The exclamation point disappears immediately.
3. Add a column type
The column type is an enumeration (General, Page, Link)
Topic type: Regular columns, single-page columns, and link columns.
Regular topic: You can add subtopics. After setting the content model, you can add corresponding content.
Single-page topic: only one page, page content can be set.
Link section: A redirection link.
InNinesky. BaseProjectRight-click->Add->Class. Input class name"CategoryType"
Modify the Code as follows:
1 using System. componentModel. dataAnnotations; 2 3 namespace Ninesky. base 4 {5 /// <summary> 6 // topic type 7 /// </summary> 8 public enum CategoryType 9 {10 [Display (Name = "regular topic" )] 11 General, 12 [Display (Name = "")] 13 pages, 14 [Display (Name = "")] 15 Link 16} 17} 18
4. Add topic model 4.1 and public model class
OpenCategory. CS,The modification code is as follows:
1 using System. componentModel. dataAnnotations; 2 3 namespace Ninesky. base 4 {5 /// <summary> 6 // topic Model 7 /// </summary> 8 public class Category 9 {10 [Key] 11 public int CategoryId {get; set;} 12 13 // <summary> 14 // topic name 15 /// </summary> 16 [Required] 17 [StringLength (50)] 18 19 public string Name {get; set ;} 20 21 /// <summary> 22 // column type 23 /// </summary> 24 [Required] 25 [Display (Name = "column type")] 26 public CategoryType Type {get; set ;} 27 28 /// <summary> 29 // ID of the upper-level column 30 /// </summary> 31 /// <remarks> 32 // 0-indicates that this topic is root topic, no parent topic 33 // </remarks> 34 [Required] 35 [Display (Name = "parent topic")] 36 public int ParentId {get; set ;} 37 38 // <summary> 39 // sort 40 /// </summary> 41 // <remarks> 42 // The smaller the number, the higher the 43 /// </remarks> 44 [Required] 45 [Display (Name = "sort")] 46 public int Order {get; set ;} 47 48 // <summary> 49 // open the target 50 /// </summary> 51 [Required] 52 [StringLength (20)] 53 [Display (Name = "open Target")] 54 public string Target {get; set ;} 55 56 // <summary> 57 // topic description 58 /// </summary> 59 [Required] 60 [StringLength (1000)] 61 [Display (Name = "")] 62 public string Description {get; set;} 63} 64}
4.2 Add a regular topic Model
InNinesky. BaseProjectRight-click->Add->Class. Input class name"CategoryGeneral", The Code is as follows:
1 using System. componentModel. dataAnnotations; 2 3 namespace Ninesky. base 4 {5 /// <summary> 6 // regular topic Model 7 /// </summary> 8 public class CategoryGeneral 9 {10 [Key] 11 public int GeneralId {get; set ;} 12 13 /// <summary> 14 // topic ID 15 /// </summary> 16 [Required] 17 [Display (Name = "topic ID")] 18 public int CategoryId {get; set;} 19 20 /// <summary> 21 // column view 22 /// </summary> 23 [Required] 24 [StringLength (200)] 25 [Display (Name = "")] 26 public string View {get; set ;} 27 28 /// <summary> 29 // Module name 30 /// </summary> 31 [Required] 32 [StringLength (50)] 33 [Display (Name = "Module Name")] 34 public string Module {get; set ;} 35 36 /// <summary> 37 // content view 38 /// </summary> 39 [Required] 40 [StringLength (200)] 41 [Display (Name = "content View")] 42 public string ContentView {get; set;} 43 44 /// <summary> 45 // content sorting 46 /// </summary> 47 [Required] 48 [StringLength (200)] 49 [Display (Name = "content sorting")] 50 public int? ContentOrder {get; set;} 51 52 // <summary> 53 // column 54 // </summary> 55 public virtual Category {get; set ;} 56} 57} 58
4.3 add a single-page topic Model
InNinesky. BaseProjectRight-click->Add->Class. Input class name"CategoryPage", The Code is as follows:
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 /// column ID 12 /// </summary> 13 [Required] 14 [Display (Name = "column ID")] 15 public int CategoryId {get; set ;} 16 17 /// <summary> 18 // column content 19 /// </summary> 20 [Required] 21 [StringLength (10000)] 22 [Display (Name = "topic Content")] 23 public string Content {get; set ;} 24 25 // <summary> 26 // column view 27 /// </summary> 28 [Required] 29 [StringLength (200)] 30 [Display (Name = "topic View")] 31 public string View {get; set ;} 32 33 // <summary> 34 // column 35 // </summary> 36 public virtual Category {get; set;} 37 38 public CategoryPage () 39 {40 View = "Index"; 41} 42} 43} 44
4.4 Add a link topic Model
InNinesky. BaseProjectRight-click->Add->Class. Input class name"CategoryLink", The Code is as follows:
1 using System. componentModel. dataAnnotations; 2 3 namespace Ninesky. base 4 {5 /// <summary> 6 // link topic Model 7 /// </summary> 8 public class CategoryLink 9 {10 [Key] 11 public int LinkId {get; set ;} 12 13 /// <summary> 14 // topic ID 15 /// </summary> 16 [Required] 17 [Display (Name = "topic ID")] 18 public int CategoryId {get; set ;} 19 20 /// <summary> 21 // topic address 22 /// </summary> 23 [Required] 24 [DataType (DataType. url)] 25 [StringLength (500)] 26 [Display (Name = "")] 27 public string Url {get; set ;}28} 29} 30
Return to Public ModelCategory. CS, Add a foreign key at the bottom
1 // <summary> 2 // topic description 3 /// </summary> 4 [Required] 5 [StringLength (1000)] 6 [Display (Name = "")] 7 public string Description {get; set ;} 8 9 // Add navigation attributes 10 /// <summary> 11 /// regular column 12 /// </summary> 13 public virtual CategoryGeneral General {get; set ;} 14 15 /// <summary> 16 // single-Page column 17 /// </summary> 18 public virtual CategoryPage {get; set ;} 19 20 /// <summary> 21 // Link topic 22 /// </summary> 23 public virtual CategoryLink Link {get; set ;} 24 // The added navigation property ends 25} 26} 27
Iii. Others
Code hosting address: https://git.oschina.net/ninesky/Ninesky
Article published by: http://www.ninesky.cn
Http://mzwhj.cnblogs.com/
Code package download: http://pan.baidu.com/s/1dFBmg0p