Files
COMCMS/COMCMS.Web/Startup.cs
T

102 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.WebEncoders;
using COMCMS.Common;
using COMCMS.Web.Models;
using COMCMS.Web.Common;
namespace COMCMS.Web
{
public class Startup
{
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)
{
//注入自己的HttpContext
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddMvc().AddSessionStateTempDataProvider();
//添加Session 服务
services.AddSession();
//部分系统配置
services.Configure<SystemSetting>(Configuration.GetSection("SystemSetting"));
services.AddMvc();
//防止汉字被自动编码
services.Configure<WebEncoderOptions>(options =>
{
options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.All);
});
//记录错误
services.AddMvc(options =>
{
options.Filters.Add<HttpGlobalExceptionFilter>();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp)
{
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
await next();
});
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//其他错误页面处理
app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
//启用Session
app.UseSession();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Index}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
//使用环境变量
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
//加入HttpContext
MyHttpContext.ServiceProvider = svp;
}
}
}