Hi @Jim Whitaker ,
One error Warning CS8600 Converting null literal or possible null value to non-nullable type.
For the CS8600 warning, this issue relates the Asp.net 6 enabled the nullable reference types:
By utilizing the new Nullable feature in C# 8, ASP.NET Core can provide additional compile-time safety in the handling of reference types. For example, protecting against null reference exceptions. Projects that have opted in to using nullable annotations may see new build-time warnings from ASP.NET Core APIs.
So, it will show the CS8600 warning. To remove this warning, you can use the following methods:
- Add the suppress in source:
public class MyClass
{
private readonly IHttpContextAccessor _contextAccessor;
public MyClass(IHttpContextAccessor httpContextAccessor)
{
_contextAccessor = httpContextAccessor;
}
public string? getValue(string mykey)
{
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
string testvalue = _contextAccessor.HttpContext.Session.GetString(mykey);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
return testvalue;
}
}
- Disable the nullable reference types.
Change the Nullable property from enable
to disabled
, after modifying the result (.csproj file)as below:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disabled</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Second, to get the data from a session, you need to use the GetString()
method (based on your code, you are storing string values), instead of the Get()
method, you can refer the following sample code:
Class:
namespace Core6MVCapp.Models
{
public class MyClass
{
private readonly IHttpContextAccessor _httpContextAccessor;
public MyClass(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string? getValue(string mykey)
{
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
string testvalue = _httpContextAccessor.HttpContext.Session.GetString(mykey);
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning restore CS8602 // Dereference of a possibly null reference.
return testvalue;
}
}
}
Program.cs:
using Core6MVCapp.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDistributedMemoryCache();
//session
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
//httpcontextaccessor
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<MyClass>();
var app = builder.Build();
...
Core6MVCapp.csproj: if you have added the suppress in source, there is no need to change the Nullable property.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>disabled</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Controller:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public const string SessionKeyName = "_Name";
public const string SessionKeyAge = "_Age";
private readonly MyClass _myclass;
public HomeController(ILogger<HomeController> logger, MyClass myClass)
{
_logger = logger;
_myclass = myClass;
}
public IActionResult Index()
{
if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName)))
{
HttpContext.Session.SetString(SessionKeyName, "The Doctor");
HttpContext.Session.SetInt32(SessionKeyAge, 73);
}
var name = HttpContext.Session.GetString(SessionKeyName);
var age = HttpContext.Session.GetInt32(SessionKeyAge).ToString();
var result = _myclass.getValue(SessionKeyName);
return View();
}
public IActionResult Privacy()
{
var result = _myclass.getValue(SessionKeyName);
return View();
}
The output is like this:
Result 1: Add the suppress in source:
Result 2: Disable the nullable reference types.
Update:
It seems that you want to access the class in Razor page, if that is the case, the code should like this:
using Core6MVCapp.Models; // the MyClass class is in the Models folder.
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Core6MVCapp.Pages
{
public class DefaultModel : PageModel
{
private readonly string SessionKeyName ="_Name";
private readonly MyClass _myClass;
public DefaultModel(MyClass myClass)
{
_myClass = myClass;
}
public void OnGet()
{
var result = _myClass.getValue(SessionKeyName);
}
}
}
Result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion