Use Study Notes for Asp.net core 2.0.1 Razor (1), 2.0.1razor

Source: Internet
Author: User

Use Study Notes for Asp.net core 2.0.1 Razor (1), 2.0.1razor

Environment: vs2017 version: 15.5.6

The Razor page mode is significantly different from that of mvc. As Microsoft officially said, "Razor pages are a new feature of ASP. NET Core MVC, which can make page-based coding easier and more efficient ."

The differences in the Code are almost identical, but the storage location is fundamentally different. The result of my personal research analysis is that the Razor page mode actually converts the Controller in mvc to zero, that is, the operation code in the original controller is distributed to the. cshtml. cs file on each page. In this way, files in the original mvc are classified by type into by function. The advantage is that each page is modularized, and the data involved in this page is here for easy maintenance, you don't need to switch between controllers, models, and views. It makes me feel like the Page Structure of the original web form. Of course, after the entire page is changed to zero, you don't need to read all the operations on the controller, performance may be improved.

At the same time, this change makes the code simple, easy to maintain, and less prone to errors, so it is worth learning.

1. Create a project

1. File, new project, Visual c #,. NET Core, ASP. NET Core Web application (". NET Framework" 4.6.1)

2. Select ". NET Core", "ASP. NET Core 2.0", and then select "Web application" (authentication type: personal user account ).

2. Modify the database connection. This is derived from the research results of brother Zhang wushui.

1. Relative Path:

Modify "ConnectionStrings" (row 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: AttachDbFilename = % CONTENTROOTPATH % \ App_Data \ aspnet123.mdf;

Using ContentRootPath is safer to place files under the project directory rather than the wwwroot directory.

ContentRootPath is used to contain application files.
WebRootPath is used to contain Web service content files.
The differences are as follows:

ContentRoot: C: \ MyApp \
WebRoot: C: \ MyApp \ wwwroot \

2. Modify Startup. cs.

Modified code:

① Modify the Startup method as follows:

Public Startup (IConfiguration configuration, IHostingEnvironment env) {Configuration = configuration;
// Add _ env = env ;}

② Add public IHostingEnvironment _ env {get ;}

③ Modify the ConfigureServices Method

Unregister the original services. AddDbContext

// Add and modify () Declare the variable conn and handle it accordingly
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 ));

Code after modification:

Public class Startup {public Startup (IConfiguration configuration, IHostingEnvironment env) {Configuration = configuration; // Add _ env = env;} public IConfiguration Configuration {get ;} // Add public IHostingEnvironment _ env {get;} // This method gets called by the runtime. use this method to add services to the container. public void ConfigureServices (IServiceCollection services) {// service S. addDbContext <ApplicationDbContext> (options => // options. useSqlServer (Configuration. getConnectionString ("DefaultConnection"); // Add and modify () Declare the variable conn and handle it accordingly. 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); 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 void Configure (IApplication Builder app, IHostingEnvironment env) {if (env. isDevelopment () {app. usemediaexceptionpage (); 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. mdf file with null content.

For your convenience, I have provided you with a sample database.

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.