码迷,mamicode.com
首页 > Web开发 > 详细

Asp.net core 2.0.1 Razor 的使用学习笔记(一)

时间:2018-02-01 23:16:17      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:configure   runtime   ddb   rate   service   index   读取   view   新功能   

环境:vs2017 版本:15.5.6

这里说明下, Razor页面模式跟mvc出现了严重的不同。正如微软官方说的一样“Razor 页面是 ASP.NET Core MVC 的一个新功能,它可以使基于页面的编码方式更简单高效。”

但就代码说没有什么不同几乎完全一样,但是存放的位置却有了根本的区别。个人研究分析的结果是:Razor页面模式其实是把mvc中的控制器化整为零了,即原来控制器中的操作代码被分布放到了各个页面的.cshtml.cs文件中了。这样一来由原来mvc中文件按类型分类变成了按功能分类,这样的好处就是每个页面形成了模块化,这个页面涉及的数据都在这里,便于维护,不用控制器、模型、视图来回切换了,给我的感觉多少有些像原来的web窗体的页面结构,当然化整为零后每个页面的操作不用全部去读取控制器,可能在性能有提升。

 

一、新建项目

1、文件》新建》项目》Visual c#》.NET Core》ASP.NET Core Web应用程序(“.NET Framework” 4.6.1)

2、依次选择“.NET Core”》“ASP.NET Core 2.0”,然后选择“Web 应用程序”(身份验证类型:个人用户账户)。

二、修改数据库连接。引自“张不水”兄的研究成果。

1、相对路径:

修改appsettings.json文件中的"ConnectionStrings"(第3行)

"DefaultConnection": "Data Source=(localdb)\\mssqllocaldb;AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true”

需注意的是:AttachDbFilename=%CONTENTROOTPATH%\\App_Data\\aspnet123.mdf;

使用 ContentRootPath 是将文件放置在项目目录下而不是wwwroot目录下,这样更安全。

ContentRootPath 用于包含应用程序文件。
WebRootPath 用于包含Web服务性的内容文件。
实际使用区别如下:

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

2、修改Startup.cs

修改后的代码:

①修改Startup方法为如下

public Startup(IConfiguration configuration,IHostingEnvironment env)
        {
            Configuration = configuration;
//新添加 _env = env; }

②添加public IHostingEnvironment _env { get; }

③修改ConfigureServices方法

注销掉原有的services.AddDbContext

//添加修改()声明变量conn并做相应处理
string conn = Configuration.GetConnectionString("DefaultConnection");
if (conn.Contains("%CONTENTROOTPATH%"))
{
conn = conn.Replace("%CONTENTROOTPATH%", _env.ContentRootPath);
}
//修改默认的连接服务为conn
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(conn));

修改完成后的代码:

技术分享图片
public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            Configuration = configuration;
            //新添加
            _env = env;
        }

        public IConfiguration Configuration { get; }
        //新添加
        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)
        {
            //services.AddDbContext<ApplicationDbContext>(options =>
            //    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            //添加修改()声明变量conn并做相应处理
            string conn = Configuration.GetConnectionString("DefaultConnection");
            if (conn.Contains("%CONTENTROOTPATH%"))
            {
                conn = conn.Replace("%CONTENTROOTPATH%", _env.ContentRootPath);
            }
            //修改默认的连接服务为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(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、手动在项目中添加“App_data”文件夹,并复制粘贴一个标准的内容为空的.mdf文件。

为方便大家学习我这里为大家提供了示例数据库

Asp.net core 2.0.1 Razor 的使用学习笔记(一)

标签:configure   runtime   ddb   rate   service   index   读取   view   新功能   

原文地址:https://www.cnblogs.com/chonghanyu/p/8400214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!