ASP.NET Core Series (2): The project to create the first .Net Core

Recall .NET Core Introduction and development environment installation, this chapter will talk about ASP.NET Core 2.0 project structure for complete ASP.NET Core series: https://www.cnblogs.com/zhangweizhong/category/ 1477144.html

 

New Project

New Project, select .NET Core There are several types of options, namely Console, ASP.NET Core empty project, Web API

We chose ASP.NET Core Web App (MVC), not marked MVC is to use Razor pages project.

 

Program structure.

The new project structure shown below, Framework and ASP.NET versions substantially similar, Controller, Model, View not introduced.

Outline the various file is used to do behind the article will do a detailed study.

launchSettings.json

As the name suggests, this is json startup configuration file format, as shown below:

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:9452",
      "sslPort": 44379
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "MyFirstCore": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

 图的上部分对应json中的profiles中定义的两种配置,分别以IIS Express。基本上都是Web服务器比如URL、身份认证以及SSL等配置。

 

wwwroot

wwwroot它包含了所有的"前端"的静态文件,  css、image、JS以及一个名为lib的文件夹。

lib中默认内容是bootstrap和jquery。

在Startup中,会调用UseStaticFiles()方法, 将此目录标记到网站根目录。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //.....        
    app.UseStaticFiles();
    //.....

}

具体静态文件的路径及相关自定义配置, 授权等后文详细研究。

 

依赖项

整个项目依赖引用的类库,这里主要分两部分, NuGet和SDK, 目前这两部分下面都只有一项.

Nuget:

包含Microsoft.AspNetCore.App, 展开它看一下, 里面MVC、Razor、EF以及SQLLite都要,

它包含了

  • ASP.NET Core 团队支持的所有包。
  • Entity Framework Core 支持的所有包。
  • ASP.NET Core 和 Entity Framework Core 使用的内部和第三方依赖关系。 

这里面是完整的AspNetCore的类库,其实这些程序集不会随着项目发布一起出现在部署包中, 不止没引用的, 包括引用的也不会. 这些已经存在于部署环境中了, 所以发布包不会变大反而会变小, 不必担心.

SDK:

SDk中包含了一项: Microsoft.NETCore.App, 它是.NET Core 的部分库。 也就是 .NETCoreApp 框架。 它依赖于更小的 NETStandard.Library

相对于上面的Microsoft.AspNetCore.App, 它同样是包含了一些程序集.但它似乎更"基础"一些.

二者异同

Microsoft.AspNetCore.App中大部分都是Microsoft.开头的一些程序集, 而Microsoft.NETCore.App中出现的大多数是我们熟悉的system.XXX的.

二者的关系就像ASP.NET相对于.NET, 此处是Asp.NetCore相对于.Net Core. 

SDK同样是一个大而全的集和, 在部署的时候, SDK中的引用依然不会出现在部署包中。

 

appsettings.json

这就是原来的framework版本的MVC的Web.config文件,里面会配置系统相关的所有的配置项,比如数据库连接等。

默认情况appsettings.json只有对于log日志的相关配置。这里就不一一说明了。

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

 

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
}

这里是简单的Main方法, 也就是应用的启动的入口, 启动后通过UseStartup<Startup>()指定下文的Startup启动文件进行启动。

 

Startup.cs

这是Mvc Core非常重要的地方, 包括加载配置, 通过依赖注入加载组件, 注册路由等都在此处进行。

默认的代码中:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

如上图所示, 默认情况下设置了两种不同状态下的错误页, 指定静态文件并且设置了路由。

在这里, 我们可以向管道中通过中间件的方式插入我们需要的工作内容。

比如我们还可以用app.UseAuthentication()来做身份验证。

 

我们使用 UseRun 和 Map 来配置 HTTP 管道。 

Use 方法可使管道短路(即不调用 next 请求委托)。 

Run 是一种约定,并且某些中间件组件可公开在管道末尾运行的 Run[Middleware] 方法。

Map* 扩展用作约定来创建管道分支。

 

此处涉及内容非常多, 比如管道机制、路由注册、身份认证等都需要专题研究。

 

小结

项目的结构大体就是这样,主要功能介绍完了,后面会一个一个详细介绍各自的功能和作用。

 

Guess you like

Origin www.cnblogs.com/zhangweizhong/p/10978440.html