Click here to Skip to main content
15,889,096 members
Home / Discussions / ASP.NET
   

ASP.NET

 
Question.Net Core 2.1, MongoDB, add states collection to country states collection Pin
jkirkerx24-Aug-18 12:31
professionaljkirkerx24-Aug-18 12:31 
Question.Net Core 2.1, seeding a MongoDB, foreach on Countries, Pin
jkirkerx24-Aug-18 9:47
professionaljkirkerx24-Aug-18 9:47 
AnswerRe: .Net Core 2.1, seeding a MongoDB, foreach on Countries, [sort of solved] Pin
jkirkerx24-Aug-18 10:25
professionaljkirkerx24-Aug-18 10:25 
General[solved] Pin
jkirkerx27-Aug-18 7:59
professionaljkirkerx27-Aug-18 7:59 
QuestionAccess HttpContext in ASP.NET Core Pin
JimFeng22-Aug-18 6:35
JimFeng22-Aug-18 6:35 
AnswerRe: Access HttpContext in ASP.NET Core Pin
Vincent Maverick Durano22-Aug-18 8:40
professionalVincent Maverick Durano22-Aug-18 8:40 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng23-Aug-18 5:07
JimFeng23-Aug-18 5:07 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Vincent Maverick Durano23-Aug-18 7:42
professionalVincent Maverick Durano23-Aug-18 7:42 
JimFeng wrote:
I'm new to ASP.NET Core and still have following questions:


  1. Where should I put ConfigureServices method?

Configuring services should be placed inside startup.cs. For more details, see: ASP.NET Core fundamentals | Microsoft Docs[^]


JimFeng wrote:
How can I inject the IActionContextAccessor and use it in my custom helper CHGridTable and other helpers?


While it may be possible, I would still recommend you to convert your static/extension class into a service whose concrete implementation would use the IHttpContextAccessor as a dependency that can be injected via its constructor just like what I've showed you on my first reply.

If you still want to follow the static class approach then you could try this:

C#
public static class HtmlHelperExt{
    private static IHttpContextAccessor _httpContextAccessor;
    private static IUrlHelperFactory _urlHelperFactory;
    public static void SetHttpContextAccessor(IHttpContextAccessor  accessor, IUrlHelperFactory urlHelperFactory) {
        _httpContextAccessor = accessor;
        _urlHelperFactory = urlHelperFactory;
    }

    public static IHtmlString CHGridTable<tmodel>(this HtmlHelper<tmodel> htmlHelper, DataTable dt, object htmlAttributes = null)
    {
            var urlHelper = _urlHelperFactory.GetUrlHelper(_httpContextAccessor.HttpContext);
            string urlEdit = urlHelper.RouteUrl("Default");
            //rest of the stuff here
    }
}


In the Startup.ConfigureServices method you can call services.BuildServiceProvider() to get the IServiceProvider to resolve the type you seek. For example:

C#
public void ConfigureServices(IServiceCollection service) {

    services.AddMvc();

    //this would have been done by the framework any way after this method call;
    //in this case you call the BuildServiceProvider manually to be able to use it
    var serviceProvider = services.BuildServiceProvider();

    //here is where you set you accessor and url helper factory
    var accessor = serviceProvider.GetService<IHttpContextAccessor>()
    var userHelperFactory = serviceProvider.GetService<IUrlHelperFactory>()
    HtmlHelperExt.SetHttpContextAccessor(accessor,userHelperFactory);

  
}

GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng23-Aug-18 8:39
JimFeng23-Aug-18 8:39 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Vincent Maverick Durano23-Aug-18 10:28
professionalVincent Maverick Durano23-Aug-18 10:28 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming23-Aug-18 9:14
mveRichard Deeming23-Aug-18 9:14 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng24-Aug-18 9:21
JimFeng24-Aug-18 9:21 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 2:02
mveRichard Deeming28-Aug-18 2:02 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng28-Aug-18 7:09
JimFeng28-Aug-18 7:09 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 7:16
mveRichard Deeming28-Aug-18 7:16 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng28-Aug-18 7:46
JimFeng28-Aug-18 7:46 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 7:56
mveRichard Deeming28-Aug-18 7:56 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng28-Aug-18 7:57
JimFeng28-Aug-18 7:57 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 8:12
mveRichard Deeming28-Aug-18 8:12 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng29-Aug-18 1:41
JimFeng29-Aug-18 1:41 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming29-Aug-18 2:44
mveRichard Deeming29-Aug-18 2:44 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng29-Aug-18 8:27
JimFeng29-Aug-18 8:27 
QuestionRegarding Request.ApplicationPath in Framework 4.5 Pin
sheelarose21-Aug-18 23:40
sheelarose21-Aug-18 23:40 
AnswerRe: Regarding Request.ApplicationPath in Framework 4.5 Pin
F-ES Sitecore21-Aug-18 23:48
professionalF-ES Sitecore21-Aug-18 23:48 
GeneralRe: Regarding Request.ApplicationPath in Framework 4.5 Pin
sheelarose21-Aug-18 23:51
sheelarose21-Aug-18 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.