Net Core MVC: Name Session does not exist in Current Context

耗尽温柔 提交于 2019-12-24 10:57:03

问题


I am receiving an error in the last line. How would I resolve this? Trying to convert Net 4.6.2 Project to MVC Net Core.

Last Line Error: Name Session does not exist in Current Context.

public class TransferPropertyOwnershipController : Controller
{
  public ActionResult GetNewOwnerRecord(int ownerID, int propertyID)
    {
        OwnerManager ownerManager = new OwnerManager();
        var newOwner = ownerManager.Get(ownerID, true);

        PropertyOwnerRoleViewModel newPropertyOwnerRoleViewModel = new PropertyOwnerRoleViewModel();
        newPropertyOwnerRoleViewModel.OwnerID = newOwner.OwnerID;
        newOwner.Address = new Model.Address();
        newPropertyOwnerRoleViewModel.Owner = newOwner;
        newPropertyOwnerRoleViewModel.IsPrimaryOwner = false;
        newPropertyOwnerRoleViewModel.OwnershipPercentage = 0;
        newPropertyOwnerRoleViewModel.PropertyID = propertyID;

        IQueryable<PropertyOwnerRoleViewModel> currentOwnersResult = Session[_currentOwnersResult] as IQueryable<PropertyOwnerRoleViewModel>;

Proposed Solution:

Number 1

HttpContext.Session.GetString(_currentOwnersResult]) 

Error Text: 'ISession' does not contain a definition for 'GetString' and no accessible extension method 'GetString' accepting a first argument of type 'ISession' could be found (are you missing a using directive or an assembly reference?)

Number 2

IQueryable<PropertyOwnerRoleViewModel> currentOwnersResult = HttpContext.Current.Session(_currentOwnersResult]) as IQueryable<PropertyOwnerRoleViewModel>;

Error Text: 'HttpContext' does not contain a definition for 'Current' and no accessible extension method 'Current' accepting a first argument of type 'HttpContext' could be found (are you missing a using directive or an assembly reference?)

Resources:

Trying to utilize this solution, but having issues. 'Session' does not exist in the current context


回答1:


You need to add service in your startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
      ....
   services.AddSession(options =>
        {

        });
....
   }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
.....
        app.UseSession();
....
    }


来源:https://stackoverflow.com/questions/55254700/net-core-mvc-name-session-does-not-exist-in-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!