微软asp.net core帮助文档
asp.net core会话
https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/app-state?view=aspnetcore-8.0
Program.cs 文件中,核心代码:
csharp
builder.Services.AddSession(options => {
//滑动过期时间,空闲多长时间没有操作会过期;
options.IdleTimeout = TimeSpan.FromSeconds(30);
});
Program.cs 完整代码
csharp
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using WebProjectNet7;
using WebProjectNet7.DataBaseEntity.Tool;
using WebProjectNet7.DependencyInjection;
using WebProjectNet7.Handler;
using WebProjectNet7.IBLL;
using WebProjectNet7.IBLL_impl;
using WebProjectNet7.IDAO;
using WebProjectNet7.IDAO_impl;
using WebProjectNet7.ViewEntity;
var builder = WebApplication.CreateBuilder(args);
//依赖注入,注册接口,修改时间:2024-2-19 13:45:17,
ServicesRegister.Register(builder);
// Views视图文件输出到发布目录
//builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews(ops =>
{
//全局异常过滤器,注册
ops.Filters.Add<ExceptionFilter>();
})
// Views视图文件输出到发布目录
.AddRazorRuntimeCompilation()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
});
builder.Services.AddSession(options => {
//滑动过期时间,空闲多长时间没有操作会过期;
options.IdleTimeout = TimeSpan.FromSeconds(30);
});
builder.Services.AddHttpContextAccessor();
//解决输入页面中文被编码了
builder.Services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
// 添加 CORS 支持,允许跨域
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
builder =>
{
builder.AllowAnyOrigin() // 允许任何来源
.AllowAnyHeader() // 允许任何头部
.AllowAnyMethod(); // 允许任何 HTTP 方法
});
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
// 启用 CORS 中间件
app.UseCors("AllowAnyOrigin");
app.UseStaticFiles();
app.UseRouting();
string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, MyConfigReader.GetConfigValue("uploadFilefolder"));
if (!System.IO.Directory.Exists(dir))
{
System.IO.Directory.CreateDirectory(dir);
}
string direxport = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, MyConfigReader.GetConfigValue("exportFilefolder"));
if (!System.IO.Directory.Exists(direxport))
{
System.IO.Directory.CreateDirectory(direxport);
}
//配置静态文件,暴露在http管道中,以供浏览器访问
app.UseStaticFiles(new StaticFileOptions()
{
RequestPath = new PathString("/" + MyConfigReader.GetConfigValue("uploadFilefolder")),
FileProvider = new PhysicalFileProvider(dir)
});
app.UseStaticFiles(new StaticFileOptions()
{
RequestPath = new PathString("/" + MyConfigReader.GetConfigValue("exportFilefolder")),
FileProvider = new PhysicalFileProvider(direxport)
});
app.UseAuthorization();
app.UseSession();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
AppServicesHelpter.App = app;
app.Run();