Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
Hi guys
I'm attempting to replicate some functionality from an old site and im wondering how do i get the CurrentContext in Umbraco 10 and the IP address?
CurrentContext
My original code is
var userIP = HttpContext.Current.Request.GetUserIPAddress();
I just cant find the equivalent code when using Umbraco 10?
Thanks for any help
Hi!
This has changed in .net core. Try using this instead:
Context.Request.HttpContext.Connection.RemoteIpAddress
Hi @Markus_Rytterkull
I dont have Context and if i do then i dont see Request, do you know what namespace i may need?
Context
Request
Could you also provide me with the source for this so i can try and have a look.
Thanks
Are you doing this in a Controller or a View?
In my views, I have access to Context automatically, which is nice.
In a controller, you have to do a few steps:
Add a private field to hold your context: private readonly IHttpContextAccessor _httpContext;
Add IHttpContextAccessor httpContextAccessor to your constructor ... , IHttpContextAccessor httpContextAccessor) : base(logger, compositeViewEngine, umbracoContextAccessor)
set your private field: _httpContext = httpContextAccessor;
So this a more a .net core thing than an umbraco thing.
Here is a controller example:
public class ContentFolderController : RenderController { private readonly IHttpContextAccessor _httpContext; public ContentFolderController(ILogger<ContentFolderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor, IHttpContextAccessor httpContextAccessor) : base(logger, compositeViewEngine, umbracoContextAccessor) { _httpContext = httpContextAccessor; } public override IActionResult Index() { //use context to get ip var userIp = _httpContext.HttpContext.Connection.RemoteIpAddress; return View("~/Views/SmartContentFolder.cshtml", CurrentPage); } }
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
How to get CurrentContext and IP address?
Hi guys
I'm attempting to replicate some functionality from an old site and im wondering how do i get the
CurrentContext
in Umbraco 10 and the IP address?My original code is
I just cant find the equivalent code when using Umbraco 10?
Thanks for any help
Hi!
This has changed in .net core. Try using this instead:
Hi @Markus_Rytterkull
I dont have
Context
and if i do then i dont seeRequest
, do you know what namespace i may need?Could you also provide me with the source for this so i can try and have a look.
Thanks
Are you doing this in a Controller or a View?
In my views, I have access to Context automatically, which is nice.
In a controller, you have to do a few steps:
Add a private field to hold your context: private readonly IHttpContextAccessor _httpContext;
Add IHttpContextAccessor httpContextAccessor to your constructor ... , IHttpContextAccessor httpContextAccessor) : base(logger, compositeViewEngine, umbracoContextAccessor)
set your private field: _httpContext = httpContextAccessor;
So this a more a .net core thing than an umbraco thing.
Here is a controller example:
is working on a reply...