Click here to Skip to main content
15,918,742 members
Home / Discussions / ASP.NET
   

ASP.NET

 
Questionpost list of products instead of single product in post method odata Pin
Asif Rehman28-Aug-18 22:30
Asif Rehman28-Aug-18 22:30 
AnswerRe: post list of products instead of single product in post method odata Pin
dan!sh 28-Aug-18 22:40
professional dan!sh 28-Aug-18 22:40 
Rant[REPOST] post list of products instead of single product in post method odata Pin
Richard Deeming29-Aug-18 2:58
mveRichard Deeming29-Aug-18 2:58 
QuestionSub Level Hyperlink not showing in asp.net Pin
Member 1115418828-Aug-18 19:13
Member 1115418828-Aug-18 19:13 
AnswerRe: Sub Level Hyperlink not showing in asp.net Pin
dan!sh 28-Aug-18 20:55
professional dan!sh 28-Aug-18 20:55 
GeneralRe: Sub Level Hyperlink not showing in asp.net Pin
Member 1115418828-Aug-18 21:04
Member 1115418828-Aug-18 21:04 
GeneralRe: Sub Level Hyperlink not showing in asp.net Pin
dan!sh 28-Aug-18 21:09
professional dan!sh 28-Aug-18 21:09 
QuestionHow to publish more than one webapps from visual studio in single click Pin
Dominic Louis27-Aug-18 20:12
Dominic Louis27-Aug-18 20:12 
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 

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.