. Net Core MVC website development (Ninesky) 2.2, column management function-added in the System area, mvc-system

Source: Internet
Author: User

. Net Core MVC website development (Ninesky) 2.2, column management function-added in the System area, mvc-system

In asp or asp.net, pages with similar functions are usually put in a folder to facilitate clear website structure, and user management functions are put in the Admin folder, all user functions are stored in the Member folder. In MVC, Region (Areas) is usually used for organization. the MVC versions earlier than. Net Core MVC are included in the Areas folder ,. net Core MVC is different from previous versions. fixed location restrictions allow controllers and views to be placed anywhere, as long as the region name is indicated on the controller.

You can create a region as follows:

1. register the region route in the Startup. cs file of the project.
  1  routes.MapRoute(  2                     name: "area",  3                     template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");  4 
2. Add the Area attribute to the Controller of the region route.
1 [Area ("region name")]

3. The general practice is to create the Areas \ region name folder in the project, then create the Controllers folder under this folder to put the controller, and the Views folder to put the view. (In fact, this is only to make the project file clear and not necessary. After completing the preceding two steps, you can place the controller and view in any position of the project ).

1. Create a System Region

Create a System region for the background management function for clear project structure in Ninesky. The procedure is as follows:

1. Register a route

Open the Startup. cs file and register the region route.

2. Create a regional folder

InNinesky. WebRight-click the project and choose add> folder, and enter the name Areas.

Right-click the Areas folder and choose add from the shortcut menu. Enter the name System.

3. Add a controller

Right-click the System folder and choose "add"> "Control List" from the shortcut menu.

Right-click the Controllers folder and choose "add"> "controller. In the dialog box, select MVC Controller-Empty and enter the following name: CategoryController.

Modify the default generated code as follows: note that the controller must add [Area ("System")]

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. threading. tasks; 5 using Microsoft. aspNetCore. mvc; 6 using Ninesky. base; 7 8 namespace Ninesky. web. areas. system. controllers 9 {10 /// <summary> 11 // topic controller 12 /// </summary> 13 [Area ("System")] 14 public class CategoryController: controller 15 {16 private NineskyDbContext _ dbContext; 17 private CategoryServi Ce _ categoryService; 18 public CategoryController (NineskyDbContext dbcontext) 19 {20 _ dbContext = dbcontext; 21 _ categoryService = new CategoryService (dbcontext); 22} 23 public IActionResult Index () 24 {25 return Content ("hellow word! "); 26} 27} 28}

Press F5 and enter/System/category/Index in the address bar of the browser. "hellow word!" is displayed !".

4. Add a layout page

Right-click the Ninesky. Web/Areas/System folder and choose add> folder and enter the name of Views.

Right-click the Views folder and choose "add"> "folder" and enter the Shared name.

Right-click the Shared folder and choose add> new item. Select the MVC view page. The default name is used.

The Code is as follows:

1 <! DOCTYPE html> 2 

 

Ii. Adjust model functions.

I have discussed whether to use the relationship or not to use the relationship in the group. Finally, I don't have any advantages or disadvantages. I just want to use the relational and non-relational databases in the same scenario. Although there is no need to use it, it is better to put the model in a separate project. Add a separate project Models.

1. Create the Ninesky. Models project.

InSolution (Ninesky)Right-clickAdd->Create a project,Select. NET Core-> Class Library (. NET Core ),Name input:Ninesky. Models

.

InNinesky. ModelsRight-click the project and choose add> folder. Enter the name Category.

2. Set BaseIn the project, Category. cs, CategoryGeneral. cs, CategoryLink. cs, CategoryPage. cs, and CategoryType. cs are copied to the Category folder of the Models project.

Change the namespace of these files to Ninesky. Models.

 

3. Add a reference to the Ninesky. Models project in Ninesky. DataLibrary. Using Ninesky. Models will be used in the code of the topic model.

Add the CategoryRepository class to the Ninesky. DataLibrary project and inherit from it. The Code is as follows:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. linq. expressions; 5 using System. threading. tasks; 6 using Ninesky. models; 7 using Microsoft. entityFrameworkCore; 8 9 namespace Ninesky. dataLibrary 10 {11 /// <summary> 12 // column warehousing 13 /// </summary> 14 public class CategoryRepository: baseRepository <Category> 15 {16 public CategoryRepository (DbContext dbContext): base (dbContext) 17 {} 18 19 /// <summary> 20 // find the topic [including navigation attributes] 21 /// </summary> 22 /// <param name = "keyValue "> Primary Key </param> 23 // <returns> column </returns> 24 public override Category Find (Expression <Func <Category, bool> predicate) 25 {26 return _ dbContext. set <Category> (). include (c => c. general ). include (c => c. page ). include (c => c. link ). singleOrDefault (predicate); 27} 28} 29} 30

====

Other code hosting address: https://git.oschina.net/ninesky/Ninesky

Article published by: http://www.ninesky.cn

                 http://mzwhj.cnblogs.com/

Download the code package: ninesky2.2 -systemcompute.rar

Returned directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.