ASP.NET Core网站初探
原文地址:https://blog.csdn.net/iml6yu/article/details/74530679
目录结构如下图
{ //以IIS Express启动 "iisSettings": { //是否启用windows身份验证 "windowsAuthentication": false, //是否启用匿名身份验证 "anonymousAuthentication": true, "iisExpress": { //IIS Express端口 "applicationUrl": "http://localhost:35617/", "sslPort": 0 } }, //运行配置项 "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "WebApplication1": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, //这里配置采用application方式启动时候的端口 "applicationUrl": "http://localhost:35618" } } }
wwwroot:
这里放着网站的静态资源(css,image,js等),可以在StartUp.cs中通过代码允许访问wwwroot,否则wwwroot目录内的静态资源文件将无法被加载(wwwroot目录有点类型RoR中的Public目录)
依赖项:不赘述,就是引用项
Controller和Views在这里不赘述,不了解MVC的可以在网上查一下
文件:
appsettings.json
同样是顾名思义——应用配置,类似于.NET Framework上的Web.Config文件,开发者可以将系统参数通过键值对的方式写在appsettings
文件中(如程序的连接字符串),
而Startup类中也在构造器中通过如下代码使得程序能够识别该文件
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); }
bower.json
看这里吧(https://segmentfault.com/a/1190000000349555),个人认为这个配置文件很鸡肋,类似angularjs这样的都无法提示
bundleconfig.json
压缩配置文件,看下面关于site.css的配置说明,定义了输入文件和输出文件,当网站发布之后,在发布目录中存在如下两个文件,一个是压缩文件,一个是源文件
发布目标目录/wwwroot/css/
site.css 和 site.min.css
// Configure bundling and minification for the project.
// More info at https://go.microsoft.com/fwlink/?LinkId=808241
[
{
"outputFileName": "wwwroot/css/site.min.css",
// An array of relative input file paths. Globbing patterns supported
"inputFiles": [
"wwwroot/css/site.css"
]
},
{
"outputFileName": "wwwroot/js/site.min.js",
"inputFiles": [
"wwwroot/js/site.js"
],
// Optionally specify minification options
"minify": {
"enabled": true,
"renameLocals": true
},
// Optionally generate .map file
"sourceMap": false
}
]
下面说两个C#的文件吧
Program.cs,在.net项目中,这个文件再熟悉不过了,程序的main函数写在这个类中,也就是程序的入口。
Startup.cs,个人理解相当于asp.net项目中的global.aspx文件,是系统的启动全局配置文件。
下面是两个比较常用的方法
// This method gets called by the runtime. Use this method to add services to the container.
//加载服务信息,例如MVC服务 public void ConfigureServices(IServiceCollection services) { // Add framework services. 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, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }
.NetCore 开发环境的安装与配置不再此赘述,请参考官方文档:https://www.microsoft.com/net/core#windowsvs2017