Environment: vs2017 Version: 15.5.6
Here, the Razor page pattern differs significantly from MVC. As Microsoft officially said, the Razor page is a new feature of ASP. NET Core MVC, which makes page-based encoding easier and more efficient. ”
But the code says nothing is almost the same, but the location of the store is fundamentally different. The result of personal research analysis is: Razor page mode is actually the piecemeal of the controller in MVC, that is, the operation code of the original controller is distributed in the. cshtml.cs file on each page. In this way, the original MVC files by type classification into a functional classification, the advantage is that each page formed a modular, the page involved in the data are here, easy to maintain, no controller, model, view to switch back and forth, give me how much like the original Web Form page structure, Of course, the operation of each page after piecemeal does not have to read all the controller, may improve performance.
First, the new project
1. Document New Project, Visual C #. NET Core ASP. NET core Web application ("4.6.1")
2. Select ". NET core" "ASP." and select "Web Application" (Authentication type: individual user account).
Second, modify the database connection. From "Zhang not Water" brother's research results.
1. Relative path:
Modify "ConnectionStrings" (line 3rd) in the Appsettings.json file
"DefaultConnection": "Data source= (localdb) \\mssqllocaldb; Attachdbfilename=%contentrootpath%\\app_data\\aspnet123.mdf;integrated security=true; Connect timeout=30; Multipleactiveresultsets=true "
Note that: attachdbfilename=%contentrootpath%\\app_data\\aspnet123.mdf;
Using Contentrootpath is more secure by placing files under the project directory instead of the Wwwroot directory.
Contentrootpath is used to include application files.
Webrootpath is used for content files that contain Web service.
The actual use difference is as follows:
Contentroot:c:\myapp\
Webroot:c:\myapp\wwwroot\
2, modify the Startup.cs
The Modified code:
① Modify the Startup method as follows
Public Startup (iconfiguration configuration,ihostingenvironment env) { configuration = Configuration;
New Add _env = env; }
② Add public ihostingenvironment _env {get;}
③ Modifying the Configureservices method
Log out of the existing services. Adddbcontext
Add modify () DECLARE variable conn and do the corresponding processing
String conn = configuration.getconnectionstring ("defaultconnection");
IF (Conn. Contains ("%contentrootpath%"))
{
conn = conn. Replace ("%contentrootpath%", _env. Contentrootpath);
}
Modify the default connection service to Conn
Services. adddbcontext<applicationdbcontext> (options =
Options. Usesqlserver (conn));
After modifying the completed code:
Public classStartup { PublicStartup (iconfiguration configuration, ihostingenvironment env) {configuration=configuration; //New Additions_env =env; } PublicIConfiguration Configuration {Get; } //New Additions PublicIhostingenvironment _env {Get; } //This method gets called by the runtime. Use this method to add services to the container. Public voidconfigureservices (iservicecollection services) {//Services. adddbcontext<applicationdbcontext> (options =//options. Usesqlserver (configuration.getconnectionstring ("defaultconnection")); //Add modify () DECLARE variable conn and do the corresponding processing stringconn = Configuration.getconnectionstring ("defaultconnection"); if(Conn. Contains ("%contentrootpath%") ) {conn= Conn. Replace ("%contentrootpath%", _env. Contentrootpath); } //Modify the default connection service to ConnServices. adddbcontext<applicationdbcontext> (options =options. Usesqlserver (conn)); Services. Addidentity<applicationuser, identityrole>() . Addentityframeworkstores<ApplicationDbContext>() . Adddefaulttokenproviders (); //ADD application services.Services. Addtransient<iemailsender, emailsender>(); Services. Addmvc (); } //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env) {if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); App. Usebrowserlink (); App. Usedatabaseerrorpage (); } Else{app. Useexceptionhandler ("/home/error"); } app. Usestaticfiles (); App. Useauthentication (); App. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller=home}/{action=index}/{id?}"); }); } }View Code
3. Manually add the "App_Data" folder to the project and copy and paste a standard empty. mdf file.
For the convenience of everyone to learn I have provided a sample database for everyone.
The use of the ASP. NET Core 2.0.1 Razor Learning Note (i)