ASP.NET Core 1.0 入门——Application Startup
2015-12-07 14:43
471 查看
var appInsights=window.appInsights||function(config){
function r(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},u=document,e=window,o="script",s=u.createElement(o),i,f;for(s.src=config.url||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)r("track"+i.pop());return r("setAuthenticatedUserContext"),r("clearAuthenticatedUserContext"),config.disableExceptionTracking||(i="onerror",r("_"+i),f=e[i],e[i]=function(config,r,u,e,o){var s=f&&f(config,r,u,e,o);return s!==!0&&t["_"+i](config,r,u,e,o),s}),t
}({
instrumentationKey:"b2e35952-fd24-45c1-a4c5-1b24703d79e7"
});
window.appInsights=appInsights;
appInsights.trackPageView();
您当前查看的页面是未经授权的转载!
如果当前版本排版错误,请前往查看最新版本:/article/5249279.html
if (document.domain.match("cnblogs.com") !=null){document.getElementById("none-cnblogs-attention").hidden="hidden"}
提示
更新时间:2016年01月20日。
编译器会查找项目文件夹下的所有
注解
可以通过设置
默认的 appsettings.json 内容如下:
这里定义的服务是基于依赖注入(Dependency Injection)的。 在ASP.NET Core 1.0中,依赖注入技术的是被大量使用的。
下面的例子中,配置了EntityFramework(用于数据访问,需要在
在调用过
警告
设置MVC的路由方式必须在
声明
ASP.NET Core 1.0 入门——Application Startup 由 勤奋的小孩 创作,采用 知识共享 署名-相同方式共享 4.0 国际 许可协议进行许可。
本许可协议授权之外的使用权限可以从 http://space.cnblogs.com/msg/send/qin-nz 处获得。
function r(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},u=document,e=window,o="script",s=u.createElement(o),i,f;for(s.src=config.url||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)r("track"+i.pop());return r("setAuthenticatedUserContext"),r("clearAuthenticatedUserContext"),config.disableExceptionTracking||(i="onerror",r("_"+i),f=e[i],e[i]=function(config,r,u,e,o){var s=f&&f(config,r,u,e,o);return s!==!0&&t["_"+i](config,r,u,e,o),s}),t
}({
instrumentationKey:"b2e35952-fd24-45c1-a4c5-1b24703d79e7"
});
window.appInsights=appInsights;
appInsights.trackPageView();
ASP.NET Core 1.0 入门——Application Startup¶
警告您当前查看的页面是未经授权的转载!
如果当前版本排版错误,请前往查看最新版本:/article/5249279.html
if (document.domain.match("cnblogs.com") !=null){document.getElementById("none-cnblogs-attention").hidden="hidden"}
提示
更新时间:2016年01月20日。
Startup 类¶
在 ASP.NET Core 1.0 中,Startup类是一个应用的入口点,我们可以为不同环境配置不同的内容。
编译器会查找项目文件夹下的所有
*.cs文件进行编译,而运行时会寻找所有命名空间下类名为
Startup的类作为启动方式。
注解
可以通过设置
project.json文件选择需要(或不需要)编译的文件和文件夹;也可以设置在不同的程序集中搜索
Startup类。
Startup类必须定义一个
Configure方法,也可以同时定义一个
ConfigureServices方法。
Startup 类的构造函数¶
构造函数,可以帮我们设置配置文件的位置,比如下面的代码设置了 appsettings.json 。public Startup(IHostingEnvironment env) { // 设置 配置文件 的位置 var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsDevelopment()) { // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 builder.AddUserSecrets(); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); }
默认的 appsettings.json 内容如下:
{ "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Verbose", "System": "Information", "Microsoft": "Information" } } }
ConfigureServices 方法¶
ConfigureServices用来定义我们使用了哪些服务,比如MVC、EF、Identity、Logging、Route;也可以自定义一些服务。
这里定义的服务是基于依赖注入(Dependency Injection)的。 在ASP.NET Core 1.0中,依赖注入技术的是被大量使用的。
下面的例子中,配置了EntityFramework(用于数据访问,需要在
appsettings.json中正确设置连接字符串)、Identity(用于身份验证/即登录)和MVC。
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddEntityFramework() .AddSqlServer() .AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); }
在调用过
ConfigureServices之后,运行时会调用
Configure方法。
Configure 方法¶
Configure方法用于加载各种需要的中间件,类似于传统的 OWIN(Open Web Interface for .NET)。
Configure方法签名必须包含
IApplicationBuilder的参数,也可以包含一些其他的五福
IHostingEnvironment和
ILoggerFactory的参数。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { //在控制台中输出log loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); //在开发环境下,显示错误详细信息 if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { //否则,导向错误页面 app.UseExceptionHandler("/Home/Error"); // 创建数据库 try { using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>() .CreateScope()) { serviceScope.ServiceProvider.GetService<ApplicationDbContext>() .Database.Migrate(); } } catch { } } app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear()); //允许访问wwwroot文件夹下的静态文件 app.UseStaticFiles(); //设置身份验证方式 app.UseIdentity(); // 设置MVC路由 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
警告
设置MVC的路由方式必须在
Configure中设置,仅在
ConfigureServices中设置是无效的。
其他资源¶
ASP.NET 官方文档声明
ASP.NET Core 1.0 入门——Application Startup 由 勤奋的小孩 创作,采用 知识共享 署名-相同方式共享 4.0 国际 许可协议进行许可。
本许可协议授权之外的使用权限可以从 http://space.cnblogs.com/msg/send/qin-nz 处获得。
相关文章推荐
- Swift Up and Running——变量和常量
- 一个牛逼哄哄的Android框架One,秒杀最火的Xutils
- iOS----支付(微信支付、支付宝支付、银联支付控件集成支付)(转)
- Android使用Handler造成内存泄露的分析及解决方法
- 使用天天模拟器开发Android应用
- IOS文件上传
- Android中SharedPreferences的总结
- IOS之Objectivc-c获取本地存储路径
- iOS键盘操作
- Android 属性动画(Property Animation)
- IOS 霓虹灯效果
- Android SDK 在线更新镜像服务器资源
- android(33)(IP拨号器)
- Unity3D与android交互
- 去掉服务器传过来价格或者其他数字小数点后面多余的0
- ios block 导致的循环引用
- datetimepicker一个不错的日历android特效
- Android开发应该重视一下Fragment
- 【android】夜间模式简单实现
- 【Android】状态栏通知Notification、NotificationManager详解