SetObjectAsJson 和 GetObjectFromJson 未显示为 .NET Core 1.0 中 HttpContext.Session 的一部分 [英] SetObjectAsJson and GetObjectFromJson not showing as part of HttpContext.Session in .NET Core 1.0

查看:24
本文介绍了SetObjectAsJson 和 GetObjectFromJson 未显示为 .NET Core 1.0 中 HttpContext.Session 的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我担心这是菜鸟的错误,但我一直在搜索,但找不到与此相关的任何内容.

I'm worried that this is a rookie mistake, but I've been searching and I can't find anything related to this.

我有一个为 Intranet 应用程序构建的 .NET Core v1.0 Web 应用程序.它已经通过用户测试并部署到生产中.

I have a .NET Core v1.0 Web App that I build for an intranet application. It's been through user tests and deployed to production.

今天有用户指出了一个小错误.自部署以来,我已经在 VS2015 中迁移了工作区,因此我下载了该项目.在初始构建时,我收到 CS1061 错误,指出 SetObjectAsJson 不是 ISession 的一部分.这是违规代码:

A user pointed out a small bug today. I have migrated Workspaces in VS2015 since deploying, so I downloaded the project. On initial build I'm getting the CS1061 Error stating that SetObjectAsJson is not part of ISession. Here is the offending Code:

public async Task<IActionResult> Index(DateTime? beginDate, DateTime? endDate, string currentLocation)
    {       

        var userId = this.User.Identity.Name;
        var userEmail = userId.Remove(0, 11) + "@roystonllc.com";
        var querystring = this.HttpContext.Request.QueryString.Value;
        var ex = _context.Engineer.Where(x => x.sEmail == userEmail);
        var engineer = await ex.FirstOrDefaultAsync();
        HttpContext.Session.SetObjectAsJson("Engineer", engineer);

所有对 SetObjectAsJson 和 GetObjectFromJson 的引用都显示无效.以下是Startup中添加的服务

All references of SetObjectAsJson and GetObjectFromJson are showing invalid. Here are the services added in Startup

public void ConfigureServices(IServiceCollection services)
    {

        var connection = @"Server=THESERVER;Database=THEDATABASE;Trusted_Connection=True;Integrated Security=false;User ID=THEUSER;Password=THEPASSWORD";
        services.AddDbContext<SCHEDULEContext>(options => options.UseSqlServer(connection));
        // Add framework services.
        services.AddMvc();
        services.AddMvcGrid();
        services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
        services.AddSession();


    }

这是我的 project.json:

Here is my project.json:

    {
    "dependencies": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Razor.Tools": {
    "version": "1.1.0-preview4-final",
   "type": "build"
},
"Microsoft.AspNetCore.Routing": "1.1.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.Extensions.Logging.Debug": "1.1.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Microsoft.NETCore.App": {
  "version": "1.1.0",
  "type": "platform"
},
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
//"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final",
//"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.1.0-preview4-final"
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
  "version": "1.0.0-preview2-final",
  "type": "build"
},
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final",
"Microsoft.DotNet.InternalAbstractions": "1.0.0",
"NonFactors.Grid.Mvc6": "1.3.0",
"NonFactors.Grid.Core.Mvc6": "1.3.0",
"Microsoft.AspNetCore.Session": "1.1.0",
"Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final"
},

    "tools": {
    "BundlerMinifier.Core": "2.2.306",
"Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.0.0-preview2-final",
  "imports": [
    "portable-net45+win8"
  ]
}
//"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final"
},

"frameworks": {
  "netcoreapp1.0": {
    "imports": [
      "dotnet5.6",
      "portable-net45+win8"
    ]
  }
},

"buildOptions": {
  "emitEntryPoint": true,
  "preserveCompilationContext": true
},

"runtimeOptions": {
  "configProperties": {
    "System.GC.Server": true
  }
},

"publishOptions": {
  "include": [
    "wwwroot",
    "**/*.cshtml",
    "appsettings.json",
    "web.config"
  ]
},

"scripts": {
  "prepublish": [ "bower install", "dotnet bundle" ],
  "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

推荐答案

SetObjectAsJsonGetObjectFromJson 不是 ASP.NET Core 的一部分.它们是可能源自 的扩展方法这篇文章.尝试通过您的解决方案进行文本搜索,也许他们所在的库没有正确引用.

SetObjectAsJson and GetObjectFromJson are not part of ASP.NET Core. They are extension methods that originate probably from this article. Try textual search through your solution, perhaps the library they sit in is just not referenced properly.

否则,将以下类添加到您的项目中:

Otherwise, add the following class to your project:

public static class SessionExtensions
{
    public static void SetObjectAsJson(this ISession session, string key, object value)
    {
        session.SetString(key, JsonConvert.SerializeObject(value));
    }

    public static T GetObjectFromJson<T>(this ISession session, string key)
    {
        var value = session.GetString(key);

        return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
    }
}

请注意,代码 (JsonConvert) 取决于 Newtonsoft.Json NuGet 包,因此您可能还需要添加该引用.

Note that the code (JsonConvert) depends on Newtonsoft.Json NuGet package so you might have to add that reference as well.

这篇关于SetObjectAsJson 和 GetObjectFromJson 未显示为 .NET Core 1.0 中 HttpContext.Session 的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆