Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create an ASP.NET Core2.0 MVC project

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to create an ASP.NET Core2.0 MVC project". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to create an ASP.NET Core2.0 MVC project.

Project introduction

The topic of the graduation thesis is a registration system for college students, mainly to complete some data maintenance in the process of college students' registration, but of course, in the end, because it is too late, many of them have not been done. The related technologies mainly used in the project are as follows:

1) Project framework: ASP.NET Core 2.0 MVC

2) ORM:Entity Framework Core (using Code First)

3) Database engine: MySQL SERVER 5.7

4) permission verification: policy-based permission verification (Policy-Based Authorization)

5) Front-end framework: AdminLte (an open source front-end UI based on Bootstrap)

6) Table control: Jquery Datatables

7) data visualization component: Echarts

8) logging: nlog

Introduction to design and implementation

The project architecture uses a multi-tier architecture, through the separation of different functional areas to achieve the relative independence of each function. After the project is built in VS, it is shown in the following figure.

01_Entity: the entity layer, which contains PSU.Entity, a system component, to store the C# object entities corresponding to the tables in the database.

02_Infrastructure: infrastructure layer, which contains two system components, PSU.EFCore and PSU.Utility. The PSU.EFCore class library completes the operation on the database by referencing Entity Framework Core. The PSU.Utility class library contains helper class files that may be used in the system development process.

03_Logic: logic layer, including PSU.Domain, PSU.Repository two system components. PSU.Domain is used to inherit the interface class library (PSU.IService) of each domain and implement the functions in the domain interface. PSU.Repository is used to implement the operations on the database contained in the PSU.Domain class library.

04_Rule: rule layer, which contains two system components, PSU.IService and PSU.Model. PSU.IService is the functional interface class library of the system domain, PSU.Model is the data congestion model corresponding to the view, and corresponds to the entity Model in the MVC schema.

Controller.PSU: controller layer,. NET Core class library, which is used to store various controller files in MVC mode.

PSU.Site: presentation layer, ASP.NET Core MVC project, project main program.

System permission verification design:

The whole system is divided into three roles, respectively, administrators, staff, student users, through the use of Area to build the page of each role, by adding Area features on the Controller to specify the role to which the current Controller belongs. When creating a user, the role field of the user will be specified. When the user logs in successfully, the Claim will be created according to the user role, and the role permission control of the current system will be realized through the custom AuthorizztionHandler.

When the user logs in successfully, the current user information will be assigned to a static class (stored using Session). For judging whether the user is logged in, the OnActionExecuting method is overridden by customizing the base class of a controller to determine whether the user is logged in. The implementation code is as follows.

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

/ / /

/ / Custom Controller Base Class

/ / /

Public class DanvicController: Controller

{

/ / /

/ / determine whether the user is logged in

/ / /

/ / /

Public override void OnActionExecuting (ActionExecutingContext filterContext)

{

If (CurrentUser.UserId = = 0)

{

String path = filterContext.HttpContext.Request.Path

FilterContext.Result = new RedirectResult ($"/ Secret/Login?ReturnUrl= {path}")

Return

}

Base.OnActionExecuting (filterContext)

}

}

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

thirty-four

thirty-five

thirty-six

thirty-seven

thirty-eight

thirty-nine

forty

forty-one

forty-two

forty-three

forty-four

forty-five

forty-six

forty-seven

forty-eight

forty-nine

fifty

fifty-one

fifty-two

fifty-three

fifty-four

fifty-five

fifty-six

fifty-seven

fifty-eight

fifty-nine

sixty

sixty-one

sixty-two

sixty-three

sixty-four

sixty-five

sixty-six

sixty-seven

sixty-eight

sixty-nine

seventy

seventy-one

seventy-two

seventy-three

seventy-four

seventy-five

seventy-six

seventy-seven

seventy-eight

seventy-nine

eighty

eighty-one

eighty-two

eighty-three

eighty-four

eighty-five

/ / /

/ / Information of the currently logged in user

/ / /

Public static class CurrentUser

{

# region Initialize

Private static IHttpContextAccessor _ httpContextAccessor

Private static ISession _ session = > _ httpContextAccessor.HttpContext.Session

Public static void Configure (IHttpContextAccessor httpContextAccessor)

{

_ httpContextAccessor = httpContextAccessor

}

# endregion

# region Attribute

/ / /

/ / user primary key

/ / /

Public static string UserOID

{

Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserOID")

Set = > _ session.SetString (CurrentUser_UserOID,! string.IsNullOrEmpty (value)? Value: "")

}

/ / /

/ / user number

/ / /

Public static long UserId

{

Get = > _ session = = null? 0: Convert.ToInt64 (_ session.GetString ("CurrentUser_UserId"))

Set = > _ session.SetString ("CurrentUser_UserId", value! = 0? Value.ToString (): "0")

}

/ / /

/ / user name

/ / /

Public static string UserName

{

Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserName")

Set = > _ session.SetString (CurrentUser_UserName,! string.IsNullOrEmpty (value)? Value: "")

}

/ / /

/ / user logs in to the account

/ / /

Public static string UserAccount

{

Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserAccount")

Set = > _ session.SetString (CurrentUser_UserAccount,! string.IsNullOrEmpty (value)? Value: "")

}

/ / /

/ / user avatar address

/ / /

Public static string UserImage

{

Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserImage")

Set = > _ session.SetString (CurrentUser_UserImage,! string.IsNullOrEmpty (value)? Value: "")

}

/ / /

/ / user role

/ / /

Public static string UserRole

{

Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserRole")

Set = > _ session.SetString (CurrentUser_UserRole,! string.IsNullOrEmpty (value)? Value: "")

}

/ / /

/ / Home address

/ / /

Public static string UserPage

{

Get = > _ session = = null? "": _ session.GetString ("CurrentUser_UserPage")

Set = > _ session.SetString (CurrentUser_UserPage,! string.IsNullOrEmpty (value)? Value: "")

}

# endregion

}

At this point, I believe you have a deeper understanding of "how to create an ASP.NET Core2.0 MVC project". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report