ASP.NET Core 中的应用程序启动 Startup
Configuration.GetConnectionString("MovieContext")
2. IHostingEnvironment 获取项目环境变量
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
HostingEnvironment = env;
}
public IHostingEnvironment HostingEnvironment { get; }
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)
{
var contentRootPath = HostingEnvironment.ContentRootPath;
var applicationName = HostingEnvironment.ApplicationName;
services.AddMvc();
}
3. IServiceCollection 是 DependencyInjection 的一个接口,它NET Core 内置的 依赖注入服务,用于注册服务。
来自using Microsoft.Extensions.DependencyInjection; 命名空间。
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//注册MovieContext访问上下文类,这样可以在控制器中使用构造函数方式注入使用
services.AddDbContext<MovieContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("MovieContext")));
services.AddTransient<IStartupFilter, RequestSetOptionsStartupFilter>();
services.AddMvc();
}
4. ConfigureServices 方法就是用于注册服务的,如上面代码
- 可选。
- 在 Configure 方法配置应用服务之前,由 Web 主机调用。
- 其中按常规设置配置选项。
5. 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();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public class AppOptions
{
public string Option { get; set; } = "Option Default Value";
}
public class RequestSetOptionsMiddleware
{
private readonly RequestDelegate _next;
private IOptions<AppOptions> _injectedOptions; public RequestSetOptionsMiddleware(
RequestDelegate next, IOptions<AppOptions> injectedOptions)
{
_next = next;
_injectedOptions = injectedOptions;
} public async Task Invoke(HttpContext httpContext)
{
Console.WriteLine("RequestSetOptionsMiddleware.Invoke"); var option = httpContext.Request.Query["option"]; if (!string.IsNullOrWhiteSpace(option))
{
_injectedOptions.Value.Option = WebUtility.HtmlEncode(option);
} await _next(httpContext);
}
}
public class RequestSetOptionsStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return builder =>
{
builder.UseMiddleware<RequestSetOptionsMiddleware>();
next(builder);
};
}
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IStartupFilter, RequestSetOptionsStartupFilter>();
services.AddMvc();
}
ASP.NET Core 中的应用程序启动 Startup的更多相关文章
- ASP.NET Core中实现单体程序的事件发布/订阅
标题:ASP.NET Core中实现单体程序的事件发布/订阅 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/10468058.html 项目源代码: ...
- Asp.Net Core 中获取应用程序物理路径(Getting the Web Root Path and the Content Root Path in ASP.NET Core)
如果要得到传统的ASP.Net应用程序中的相对路径或虚拟路径对应的服务器物理路径,只需要使用使用Server.MapPath()方法来取得Asp.Net根目录的物理路径,如下所示: // Classi ...
- ASP.NET Core 中使用 Hangfire 定时启动 Scrapyd 爬虫
用 Scrapy 做好的爬虫使用 Scrapyd 来管理发布启动等工作,每次手动执行也很繁琐;考虑可以使用 Hangfire 集成在 web 工程里. Scrapyd 中启动爬虫的请求如下: curl ...
- 探索ASP.Net Core 3.0系列六:ASP.NET Core 3.0新特性启动信息中的结构化日志
前言:在本文中,我将聊聊在ASP.NET Core 3.0中细小的变化——启动时记录消息的方式进行小的更改. 现在,ASP.NET Core不再将消息直接记录到控制台,而是正确使用了logging 基 ...
- ASP.NET Core 中文文档 第三章 原理(1)应用程序启动
原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...
- [08]ASP.NET Core 中 launchsettings.json 启动配置文件
ASP.NET Core launchsettings.json 启动配置文件 本文作者:梁桐铭- 微软最有价值专家(Microsoft MVP) 文章会随着版本进行更新,关注我获取最新版本 本文出自 ...
- 在docker中运行ASP.NET Core Web API应用程序
本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...
- 在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序
前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1.1的地表最强IDE工具,越来越受.NET系的开发人员追捧. 随着Google Angular4的发布 ...
- 【Asp.Net Core】在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序
前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1.1的地表最强IDE工具,越来越受.NET系的开发人员追捧. 随着Google Angular4的发布 ...
随机推荐
- Linux操作系统多线程信号总结
linux 多线程信号编程总结 linux 多线程信号总结(一) 1. 在多线程环境下,产生的信号是传递给整个进程的,一般而言,所有线程都有机会收到这个信号,进程在收到信号的的线程上下文执行信号处理函 ...
- Jmeter的参数配置
Ramp-up Period(in seconds) [1]决定多长时间启动所有线程.如果使用10个线程,ramp-up period是100秒,那么JMeter用100秒使所有10个线程启动并运行. ...
- UE4 的json读写方式
转自:http://blog.csdn.net/cartzhang/article/details/41009343 JSON 的解析有很多开源库.UE4的JSON使用在代码的Public->S ...
- 在SUSE Linux Enterprise 11 SP1上用UDEV SCSI配置ASM
1. 编辑/etc/scsi_id.config文件,如果该文件不存在,则创建该文件,添加如下行: options=–whitelisted –replace-whitespace 2. 获取需要绑定 ...
- 九、创建Slave节点
通常情况下,我们的项目会由多个模块或者系统组成,不同模块可能会分别部署在不同的服务器,比如mod1部署在ser1,mod2部署在ser2上: 之前的文档是描述了将jenkins也部署在ser1上,当m ...
- tensorflow 卷积神经网络基本参数()
目录: 1. tf.placeholder_with_default(tf.constant(1.0),shape=[],name='use_dropout') # 设置一个占位符 2. tf.c ...
- 怎么改变Eclipse中项目的运行服务器
eclipse中-->选定项目-->右键-->弹出 Properties for项目名 -->选择server子项-->选择需要的server即可(Always use ...
- 牛X的FieldBlur
[牛X的FieldBlur] Use Field Blur to build a gradient of blurs, by defining multiple blur points with di ...
- mysql查询大于X分钟数
select * from table where date_add(STR_TO_DATE(createtime,'%Y-%m-%d %T:%i:%s'), interval '00:60:00 ...
- css position说明
absolute 生成绝对定位的元素,选择第一个position不等于 static 定位的第一个父元素进行定位. 元素的位置通过 "left", "top", ...