Official study Materials
Building an empty MVC Framework 1. Create an empty template
2. Create folders Controllers, Models, Views3. Create HomeController.cs under the Controllers folder
Select the Controllers folder, right-add-new item, select the MVC controller class, add HomeController.cs.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Http;usingMICROSOFT.ASPNETCORE.MVC;namespacerazormvc.controllers{ Public classHomecontroller:controller {//Get:home PublicActionResult Index () {returnView (); } }}
4. Create HomeModel.cs under the Models folder
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace razormvc.models{ publicclass homemodel { }}
5. Add _layout.cshtml in the Views folder
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0"> <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge"> <title>@ViewData ["Title"]</title></Head><Body>@RenderBody () @RenderSection ("Scripts", false)</Body></HTML>
6. Add _viewstart.cshtml in the Views folder
@{ "~/views/_layout.cshtml";}
7. Add _viewimports.cshtml in the Views folder
*, Microsoft.AspNetCore.Mvc.TagHelpers
8. Adjust Startup.cs
Public voidconfigureservices (iservicecollection services) {services. Addmvc ();//Registration Services} Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } Else{app. Useexceptionhandler ("/home/error"); App. Usehsts (); } app. Usehttpsredirection (); App. Usestaticfiles (); App. Usecookiepolicy (); App. Usesession (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); });}
9. The adjustment can be saved as a template (you can skip, wait for it to be finished together) EF Core build 1. Install EF Core
- Tools > NuGet Package Manager > Package Manager console
- Run
Install-Package Microsoft.EntityFrameworkCore.SqlServer
- Run
Install-Package Microsoft.EntityFrameworkCore.Tools
- Run
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
- Listen up, all of you.
2. Create a model from an existing database
Scaffold-dbcontext"server=.;D Atabase=test;uid=test;pwd=test; " Microsoft.entityframeworkcore.sqlserver-outputdirmodels
You can -Tables
specify which tables you want to generate entities for by adding parameters to the preceding command. For example -Tables Blog,Post
.
3. Deleting an inline context configuration
- Open it
Models\BloggingContext.cs
- Delete
OnConfiguring(...)
method
4. Registering and configuring the context in Startup.cs
Public void configureservices (iservicecollection services) { services. Addmvc (); var @" server=.;D Atabase=test;uid=test;pwd=test; " ; Services. Adddbcontext<testContext> (Options and options). Usesqlserver (connection));}
5. System Generation test Page
- In Solution Explorer, right-click the Controllers folder, and then select Add, Controller ...
- Select View uses the Entity Framework's MVC controller, and then click OK
- Set model class to "Test" and "Data context class" to "TestContext"
- Click "Add"
- Run up to test database connections
Using Redis storage session
Modify the configuration file according to your needs, here directly https://www.cnblogs.com/shouwu/p/6180652.html,
1. Add a NuGet Package
Microsoft.AspNetCore.Session; Microsoft.AspNetCore.DataProtection.Redis; Microsoft.Extensions.Caching.Redis.Core; Microsoft.AspNetCore.Http; // extended method when using session
2. Add Redis configuration in Appsettings.json
{ "Logging": { "LogLevel": { "Default":"Warning" } }, "allowedhosts":"*", "Webconfig": { "Redis": { "Connection":"ip:6379,allowadmin=true,password=test", "InstanceName":"name" }, "Sessiontimeout":" -" }}
3. In the Startup.cs class, add the code as shown in the following example
Public Get ; } Public Startup (iconfiguration configuration) { = configuration;}
Configureservices () Increase
#regionSave session with RedisvarRedisconn = configuration["WebConfig:Redis:Connection"];varRedisinstancename = configuration["WebConfig:Redis:InstanceName"];//long minutes after the Session expiresvarSessionouttime = configuration.getvalue<int> ("Webconfig:sessiontimeout", -);varRedis =StackExchange.Redis.ConnectionMultiplexer.Connect (redisconn); services. Adddataprotection (). Persistkeystoredis (Redis,"Dataprotection-test-keys"); services. Adddistributedrediscache (Option={ //Redis Connection StringOption. Configuration =Redisconn; //Redis Instance NameOption. INSTANCENAME =redisinstancename;});#endregion//add session and set expiration durationServices. Addsession (options = options. IdleTimeout = Timespan.fromminutes (sessionouttime); });
Configure () in the app. Usemvc before increase
App. Usesession ();
Redis Storage Session
HttpContext.Session.SetString ("UserName", UserName); HttpContext.Session.GetString ("UserName");
General Storage Redis
Public Get Set ; } Public Mytoolcontroller (Idistributedcache rcache) { = Rcache;} _rcache.setstring ("UserName", UserName); _rcache.getstring (" UserName");
additional
. NET Core 2.1 Mvc+ef+redis Build