❔ ASP.NET to .net Core
Im trying to convert my asp.net project to .net core. I have a class thats using Httpcontext in some getter functions. I cant convert this class to .net core class. Can you help me ?
public static User CurrentUser { get { var ck = HttpContext.Request.Cookies[TOKEN_COOKIE_NAME]; Guid token; if (ck != null && Guid.TryParse(ck.Value, out token)) { var key = "BlueCMSUser" + token; if (HttpContext.Current.Cache[key] == null) { var user = User.GetByToken(token); if (user != null) { HttpContext.Current.Cache.Add(key, user, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(15), System.Web.Caching.CacheItemPriority.Default, null); } else { RemoveToken(); } } return HttpContext.Current.Cache[key] as User; } return null; } set { var ck = HttpContext.Current.Request.Cookies[TOKEN_COOKIE_NAME]; Guid token; if (ck != null && Guid.TryParse(ck.Value, out token)) { var key = "CMSUser" + token; HttpContext.Current.Cache[key] = value; } } } this is the code im trying to convert
public static User CurrentUser { get { var ck = HttpContext.Request.Cookies[TOKEN_COOKIE_NAME]; Guid token; if (ck != null && Guid.TryParse(ck.Value, out token)) { var key = "BlueCMSUser" + token; if (HttpContext.Current.Cache[key] == null) { var user = User.GetByToken(token); if (user != null) { HttpContext.Current.Cache.Add(key, user, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(15), System.Web.Caching.CacheItemPriority.Default, null); } else { RemoveToken(); } } return HttpContext.Current.Cache[key] as User; } return null; } set { var ck = HttpContext.Current.Request.Cookies[TOKEN_COOKIE_NAME]; Guid token; if (ck != null && Guid.TryParse(ck.Value, out token)) { var key = "CMSUser" + token; HttpContext.Current.Cache[key] = value; } } } this is the code im trying to convert
21 Replies
is there anyway i can use HttpContext directly without using constructor like this ? private readonly HttpContext _httpContext;
public User(IHttpContextAccessor httpContextAccessor, IMemoryCache memoryCache)
{
_httpContext = httpContextAccessor.HttpContext;
_cache = memoryCache;
}
HttpContext
is now a property on your controllers.
If you need it outside of your controller, you inject the accessor as above
also, please use $code when showing blocksTo post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/is there anyway i can use HttpContext directlyOnly in a controller. Not outside it.
BlazeBin - lionuojixqaq
A tool for sharing your source code with the world!
was specifically this I wanted
Use the
IHttpContextAccessor
if you need access outside of a controller.
Thats all I can say, as its the answer.
No suitable constructor was found for entity type 'User'. The following constructors had parameters that could not be bound to properties of the entity type:
Cannot bind 'httpContextAccessor' in 'User(IHttpContextAccessor httpContextAccessor, IMemoryCache memoryCache)'
Note that only mapped properties can be bound to constructor parameters. Navigations to related entities, including references to owned types, cannot be bound.'
im getting this error
This is some fairly cursed code my friend
User
appears to be an entity model, and should absolutely not have a static CurrentUser
propertySo my approach is wrong .. what is the best approach here ?
I'd probably use something like...
yes you are right
then you add a similar prop/method on this
the idea is you dont want to mix your entity with your httpcontext logic
okay then i split my functions related to httpcontext to a different class
yeah, exactly
keep User as a domain entity
oh okay thanks for help
give it a spin
okay let me try ^^
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.