本項目使用的是 visual studio 2017和.net core 2.1的版本。
ASP.NET Core Web應用被創建后,項目中會產生兩個文件:Program.cs、Startup.cs,程序中把Program.cs作為Web應用程序的入口,程序啟動時會調用Startup.cs類。
ASP.NET MVC、WebApi中 Global.asax、FilterConfig.cs 和 RouteConfig.cs等類都被Program.cs、Startup.cs取而代之。
Startup.cs的作用是:在項目中用到的靜態文件、管道、服務、日志、路由、數據庫連接、過濾器的注冊等所有有關程序運行時使用。
項目Startup.cs類:
public class Startup { public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")) ); services.AddMvc(); //自動添加服務依賴 //AddTransient 每次注冊服務,會創建新的倉庫,以保證倉庫之間獨立 //倉庫依賴注入系統 services.AddTransient<INoodleRepository, NoodleRepository>(); services.AddTransient<IFeedbackRepository,FeedbackRepository>(); } // 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.UseMvcWithDefaultRoute(); 默認路由 app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(route => { route.MapRoute("default","{controller=Home}/{action=Index}/{id?}"); }); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } }
從上面可以看出Startup.cs主要包含兩個方法:ConfigureServices、Configure
1.ConfigureServices方法:將服務注冊到容器。eg:第三方組件
services.AddMvc(); //注入MVC模塊,包含MvcCore和常用的第三方庫的服務和方法
services.AddTransient<INoodleRepository, NoodleRepository>(); //自動添加服務依賴。瞬時注入,每次都會創建一個新的對象,以保證對象之間的獨立性
services.AddSingleton<IStudentRepository,MockStudentRepository>(); //單例注入,創建的對象在所有的地方所有的請求會話創建的都是相同的
services.AddScoped<IStudentRepository,MockStudentRepository>(); //作用域注入,創建的對象在同一個請求會話時是相同的
2.Configure方法:配置Http請求管道。 eg:session、cookie
HostingEnvironment.IsDevelopment(); //判斷當前運行環境是否是 Microsoft,如果是則返回true。
//如果要判斷其他運行環境,比如 Linux,則使用 env.IsEnvironment("environmentname") environmentname為要驗證的環境名稱。
app.UseStaticFiles(); //使用靜態文件
//使用MVC管道路徑,可以在這個配置路由等操作
app.UseMvc(route =>
{
route.MapRoute("default","{controller=Home}/{action=Index}/{id?}");
});
實戰:
1. .net core使用session:
ConfigureServices方法: services.AddSession(); //注入session
Configure方法:app.UseSession();
Controller:
//[Route("[controller]/[action]")] public class HomeController : Controller { // GET: /<controller>/ public IActionResult Index() { HttpContext.Session.SetString("code","123456"); return View(); } public String About() { ViewBag.Code = HttpContext.Session.GetSession("code"); return View(); } }
2. .net core使用Cache:
ConfigureServices方法: services.AddMemoryCache(); //注冊緩存服務
Controller:
public class HomeController : Controller { private IMemoryCache _cache; public HomeController (IMemoryCache memoryCache ) { _cache = memoryCache ; } }
設置緩存: _cache.set("key","value");
移除緩存: _cache.remove("key");