我正在将一个较旧的小型mvc5应用程序移植到.Net Core 2.0和MVC 6,更多地是作为练习来学习如何做。
在该应用程序中,我有一个Base Controller类,其主要工作是确保布局模型中包含用户的配置文件对象。
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (User.Identity.IsAuthenticated && Session["LayoutViewModel"] == null)
{
var lvm = new LayoutViewModel { AppUserId = User.Identity.GetUserId() };
lvm.LoggedInUserProfile =
Services.UserService.UserHelpers.GetCompleteProfileForLoggedInUser(lvm.AppUserId);
if (lvm.LoggedInUserProfile != null)
{
Session["LayoutViewModel"] = lvm;
}
else
{
Session["LayoutViewModel"] = null;
}
}
base.OnActionExecuted(filterContext);
}
我知道UserManager中获取UserId的新方法,但是在弄清楚如何设置Session变量时遇到麻烦,即使该方法在.Net Core 2.0中也可行
参考方案
您需要添加Microsoft.AspNetCore.Session Nuget包并在ConfigureServices
中注册会话服务:
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
});
之后,您将可以访问HttpContext.Session
属性。
我们正在将ASP.NET Core 2.1与.NET Framework 4.6.2结合使用。我们有一个客户需要向我们的Web应用程序发送一个很大程度上嵌套的json结构。当他们进行此调用时,我们将输出以下日志并返回错误: 读取器的MaxDepth超过了32。路径“ super.long.path.to property”,第1行,位置42111。”我浏览了…
从Axios请求以ASP.NET Core API返回下载文件 - c#大家好我正在尝试从Axios Request从ASP.NET Core Web API下载文件。这是我的示例API方法。 (基于此stackoverflow question的代码)[HttpPost("download")] public async Task<IActionResult> DownloadFile(){ .…
从.NET Core 2.1降级到.NET 4.7.1时,如何使用IApplicationBuilder和IServiceCollection? - c#我不得不将我的项目从.NET Core 2.1更改为.NET 4.7.1,并且修复了几乎所有错误,但以下错误仍然困扰着我 “ IApplicationBuilder”不包含“ UseHsts”的定义,也找不到找不到接受类型为“ IApplicationBuilder”的第一个参数的扩展方法“ UseHsts”(是否缺少using指令或程序集引用?) “ IA…
无法在ASP.NET Core 2.0中发布原始类型 - c#我正在将非常简单的json数据发布到.net Core 2.0 API。当我有这样的方法时,为什么会这样:public async Task<IActionResult> GetNewToken([FromBody]string id) 然后id为null,但是如果我将其封装在模型中:public class RandomViewModel { …
如何获取.NET Core 3单文件应用程序以查找appsettings.json文件? - c#如何配置单文件.Net Core 3.0 Web API应用程序以查找与建立单文件应用程序相同的目录中的appsettings.json文件?跑步后dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true 该目录如下所示:XX/XX/XXXX XX:XX PM <DIR> . X…