ASP.NET MVC Core 项目文件夹解读
一、项目文件夹总览
1.1、Properties——launchSettings.json
启动配置文件,你可以在项目中“Properties”文件夹中找到该文件。launchSettings.json
文件为一个ASP.NET Core应用保存特有的配置标准,用于应用的启动准备工作,包括环境变量,开发端口等。在launchSettings.json
文件中进行配置修改,和开发者右键项目——属性中所提交的更改的效果是一样的(目前右键属性中的Property真是少得可怜),并且支持同步更新。此文件设置了Visual Studio可以启动的不同环境,以下是示例项目中launchSettings.json文件生成的默认代码:
{ "iisSettings": { "windowsAuthentication": false, //启动windows身份验证,默认为false "anonymousAuthentication": true, //启用匿名身份验证,默认为true "iisExpress": { "applicationUrl": "http://localhost:28869",//应用程序启动的URL路径 "sslPort": 44318//启用SSL端口 } }, "profiles": { "IIS Express": { "commandName": "IISExpress",//传递命令参数 "launchBrowser": true,//是否在浏览器启动 "environmentVariables": {//将环境变量设置为键值对 "ASPNETCORE_ENVIRONMENT": "Development" } }, "TR.COM.WebSite.Admin": {//选择本地自宿主启动,详见Program.cs文件,删除该节点将导致VS启动选项缺失 "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000",//本地自宿主URL "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
在这里,有两个配置节点:“IIS Express”、“TR.COM.WebSite.Admin”,这两个节点,分别对应Visual Stuido的开始调试按钮的下拉选项,您可以选择对应的选项来启动应用程序:
launchSettings.json 文件用于设置在 Visual Stuido 运行应用程序的环境。我们也可以添加节点,该节点名称会自动添加到 Visual Stuido 调试按钮的下拉选项中。
要获取其它更多属性的详细信息,请转到此链接:http://json.schemastore.org/launchsettings 。
1.2、wwwroot
wwwroot
是一个存放静态内容的文件夹,存放了诸如css,js,img等文件。
1.3、appsettings
同样是顾名思义——应用配置,类似于.NET Framework上的Web.Config文件,开发者可以将系统参数通过键值对的方式写在appsettings
文件中(如程序的连接字符串),而Startup类中也在构造器中通过如下代码使得程序能够识别该文件。
public Startup(IConfiguration configuration) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); }
appsettings默认的设置如下:
{ "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*" }
1.4、Startup.cs
Startup.cs文件是ASP.NET Core的启动入口文件,想必尝试过OWIN开发的一定不会陌生。项目运行时,编译器会在程序集中自动查找Startup.cs文件读取启动配置。除了构造函数外,它可以定义Configure和ConfigureServices方法。默认的startup.cs设置如下
public class Startup { //用来启动配置文件Configuration 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?}"); }); } }
startup.cs的设置说明:
(1) 构造函数:用来启动配置文件Configuration
public Startup(IConfiguration configuration) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); }
(2) ConfigureServices
ConfigureServices 用来配置我们应用程序中的各种服务,它通过参数获取一个IServiceCollection 实例并可选地返回 IServiceProvider。ConfigureServices 方法需要在 Configure 之前被调用。更多内容请见官方文档
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); }
(3) Configure
Configure 方法用于处理我们程序中的各种中间件,这些中间件决定了我们的应用程序将如何响应每一个 HTTP 请求。它必须接收一个IApplicationBuilder参数,我们可以手动补充IApplicationBuilder的Use扩展方法,将中间件加到Configure中,用于满足我们的需求。
// 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?}"); }); }
原文地址:https://www.cnblogs.com/qtiger/p/9406666.html