using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Util.Datas.Ef;
using Util.Logs.Extensions;
using Util.Samples.Data;
using Util.Samples.RazorPageExtensions;
using Util.Webs.Extensions;
namespace Util.Samples {
///
/// 启动配置
///
public class Startup {
///
/// 初始化启动配置
///
/// 配置
public Startup( IConfiguration configuration ) {
Configuration = configuration;
}
///
/// 配置
///
public IConfiguration Configuration { get; }
///
/// 配置服务
///
public void ConfigureServices( IServiceCollection services ) {
//注册Razor视图解析路径
services.AddRazorViewLocationExpander();
//添加Mvc服务
services.AddMvc( options => options.EnableEndpointRouting = false
//options.Filters.Add( new AutoValidateAntiforgeryTokenAttribute() );
).AddNewtonsoftJson().AddRazorPageConventions();
//添加NLog日志操作
services.AddNLog();
//添加EF工作单元
//====== 支持Sql Server 2012+ ==========
services.AddUnitOfWork( Configuration.GetConnectionString( "DefaultConnection" ) );
//======= 支持Sql Server 2005+ ==========
//services.AddUnitOfWork( builder => {
// builder.UseSqlServer( Configuration.GetConnectionString( "DefaultConnection" ), option => option.UseRowNumberForPaging() );
//} );
//======= 支持PgSql =======
//services.AddUnitOfWork( Configuration.GetConnectionString( "PgSqlConnection" ) );
//======= 支持MySql =======
//services.AddUnitOfWork( Configuration.GetConnectionString( "MySqlConnection" ) );
//添加Swagger
services.AddSwaggerGen( options => {
options.SwaggerDoc( "v1", new OpenApiInfo { Title = "Util Api Demo", Version = "v1" } );
options.IncludeXmlComments( Path.Combine( AppContext.BaseDirectory, "Util.Samples.AntDesign.xml" ) );
} );
}
///
/// 配置开发环境请求管道
///
public void ConfigureDevelopment( IApplicationBuilder app ) {
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware( new WebpackDevMiddlewareOptions {
HotModuleReplacement = true
} );
app.UseSwaggerX();
CommonConfig( app );
}
///
/// 配置生产环境请求管道
///
public void ConfigureProduction( IApplicationBuilder app ) {
app.UseExceptionHandler( "/Error" );
app.UseHsts();
CommonConfig( app );
}
///
/// 公共配置
///
private void CommonConfig( IApplicationBuilder app ) {
app.UseErrorLog();
app.UseStaticFiles();
app.UseAuthentication();
ConfigRoute( app );
}
///
/// 路由配置,支持区域
///
private void ConfigRoute( IApplicationBuilder app ) {
app.UseMvc( routes => {
routes.MapSpaFallbackRoute( "spa-fallback", new { controller = "Home", action = "Index" } );
} );
}
}
}