Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Access HttpContext in ASP.NET Core

Access HttpContext in ASP.NET Core

Scheduled Pinned Locked Moved ASP.NET
asp-netcsharpdotnetarchitecturetutorial
18 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JimFeng

    Thanks again for your patient. when I code following: var urlHelper = _urlHelperFactory.GetUrlHelper(_httpContextAccessor.HttpContext); and got the following error: Argument 1: cannot convert from 'Microsoft.AspNetCore.Http.HttpContext' to 'Microsoft.AspNetCore.Mvc.ActionContext' the error was caused by argument: _httpContextAccessor.HttpContext

    V Offline
    V Offline
    Vincent Maverick Durano
    wrote on last edited by
    #7

    Hmm.. it's really hard to test it out without a dev machine. Anyway instead of passing IHttpContextAccessor, try to pass the IActionContextAccessor instead. For example in your Helper class, you can do:

    private static IActionContextAccessor \_actionContextAccessor;
    private static IUrlHelperFactory \_urlHelperFactory;
    public static void SetHttpContextAccessor(IActionContextAccessor accessor, IUrlHelperFactory urlHelperFactory) {
        \_actionContextAccessor = accessor;
        \_urlHelperFactory = urlHelperFactory;
    }
    

    then you can do:

    var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);

    and finally, configure the IActionContextAccessor in Startup.ConfigureServices method:

    var accessor = serviceProvider.GetService()
    var userHelperFactory = serviceProvider.GetService()
    HtmlHelperExt.SetHttpContextAccessor(accessor,userHelperFactory);
    

    You could also try what's suggested by Richard.

    1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      asp.net core - how to get IUrlHelper from inside Html helper - Stack Overflow[^] Correcting the typo in that answer gives:

      public static IHtmlContent CHGridTable<TModel>(this IHtmlHelper<TModel> htmlHelper, DataTable dt, object htmlAttributes = null)
      {
      var urlHelperFactory = (IUrlHelperFactory)htmlHelper.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelperFactory));
      var urlHelper = urlHelperFactory.GetUrlHelper(htmlHelper.ViewContext);
      string urlEdit = url.RouteUrl("Default");
      ...
      }

      You might also want to consider whether a custom tag helper might be a better choice for what you're trying to do: Tag Helpers in ASP.NET Core | Microsoft Docs[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      J Offline
      J Offline
      JimFeng
      wrote on last edited by
      #8

      Thanks, I corrected error in my custom helper, but when I call the helper from views as following: @HtmlHelperExt.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" }) and got following error: Error CS0411 The type arguments for method 'HtmlHelperExt.CHGridTable(HtmlHelper, DataTable, object)' cannot be inferred from the usage. Try specifying the type arguments explicitly. The call on MVC5 works file, How can I use the custom helper on view of Core Web Application (MVC6): The custom helper code as following: public static IHtmlContent CHGridTable(this HtmlHelper htmlHelper, DataTable dt, object htmlAttributes = null) { var urlHelperFactory = (IUrlHelperFactory)htmlHelper.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelperFactory)); var url = urlHelperFactory.GetUrlHelper(htmlHelper.ViewContext); ...... return new HtmlString(tbBtnAdd.ToString() + tbGridTable.ToString() + script.ToString()); }

      Richard DeemingR 1 Reply Last reply
      0
      • J JimFeng

        Thanks, I corrected error in my custom helper, but when I call the helper from views as following: @HtmlHelperExt.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" }) and got following error: Error CS0411 The type arguments for method 'HtmlHelperExt.CHGridTable(HtmlHelper, DataTable, object)' cannot be inferred from the usage. Try specifying the type arguments explicitly. The call on MVC5 works file, How can I use the custom helper on view of Core Web Application (MVC6): The custom helper code as following: public static IHtmlContent CHGridTable(this HtmlHelper htmlHelper, DataTable dt, object htmlAttributes = null) { var urlHelperFactory = (IUrlHelperFactory)htmlHelper.ViewContext.HttpContext.RequestServices.GetService(typeof(IUrlHelperFactory)); var url = urlHelperFactory.GetUrlHelper(htmlHelper.ViewContext); ...... return new HtmlString(tbBtnAdd.ToString() + tbGridTable.ToString() + script.ToString()); }

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #9

        JimFeng wrote:

        @HtmlHelperExt.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })

        That should probably be:

        @Html.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })

        You need to call it as an extension method on the IHtmlHelper instance, not call the static method directly.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        J 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          JimFeng wrote:

          @HtmlHelperExt.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })

          That should probably be:

          @Html.CHGridTable(Model, new { tablename = "tblTest", defaultcolsort = "3" })

          You need to call it as an extension method on the IHtmlHelper instance, not call the static method directly.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          J Offline
          J Offline
          JimFeng
          wrote on last edited by
          #10

          @Html.CHGridTable(......) works on MVC5 but not ASP.NET Core MVC.

          Richard DeemingR 1 Reply Last reply
          0
          • J JimFeng

            @Html.CHGridTable(......) works on MVC5 but not ASP.NET Core MVC.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #11

            Have you imported the tag helpers from your assembly? /Views/_ViewImports.cshtml:

            @addTagHelper *, YourAssemblyNameHere

            Managing Tag Helper scope | Tag Helpers in ASP.NET Core | Microsoft Docs[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            J 2 Replies Last reply
            0
            • Richard DeemingR Richard Deeming

              Have you imported the tag helpers from your assembly? /Views/_ViewImports.cshtml:

              @addTagHelper *, YourAssemblyNameHere

              Managing Tag Helper scope | Tag Helpers in ASP.NET Core | Microsoft Docs[^]


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              J Offline
              J Offline
              JimFeng
              wrote on last edited by
              #12

              I added @addTagHelper *, HtmlHelperExt in _ViewImports.cshtml file but still got error: Error: CS1061 'IHtmlHelper' does not contain a definition for 'CHDropdownMenu' and no accessible extension method 'CHDropdownMenu' accepting a first argument of type 'IHtmlHelper' could be found (are you missing a using directive or an assembly reference?) on the View page like this (the same as on MVC 5): @model System.Data.DataSet @using HtmlHelperExt; ...... @Html.CHDropdownMenu(Model, "mnuSample1", new { MenuOpenType = "Hover" }) ...... My Custom helper like this: namespace HtmlHelperExt { public static class HtmlHelperExt { public static IHtmlContent CHDropdownMenu(this HtmlHelper htmlHelper, DataSet ds, string divID, object htmlAttributes = null) { ...... return new HtmlString(tbDiv_Menu.ToString()); } } }

              Richard DeemingR 1 Reply Last reply
              0
              • J JimFeng

                I added @addTagHelper *, HtmlHelperExt in _ViewImports.cshtml file but still got error: Error: CS1061 'IHtmlHelper' does not contain a definition for 'CHDropdownMenu' and no accessible extension method 'CHDropdownMenu' accepting a first argument of type 'IHtmlHelper' could be found (are you missing a using directive or an assembly reference?) on the View page like this (the same as on MVC 5): @model System.Data.DataSet @using HtmlHelperExt; ...... @Html.CHDropdownMenu(Model, "mnuSample1", new { MenuOpenType = "Hover" }) ...... My Custom helper like this: namespace HtmlHelperExt { public static class HtmlHelperExt { public static IHtmlContent CHDropdownMenu(this HtmlHelper htmlHelper, DataSet ds, string divID, object htmlAttributes = null) { ...... return new HtmlString(tbDiv_Menu.ToString()); } } }

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #13

                JimFeng wrote:

                IHtmlHelper<dataset>' does not contain a definition for 'CHDropdownMenu'

                CHDropdownMenu<tmodel>(this HtmlHelper<tmodel> htmlHelper,

                You need to declare the helper method as an extension method against the IHtmlHelper interface, not the HtmlHelper class.

                public static IHtmlContent CHDropdownMenu<TModel>(this IHtmlHelper<TModel> htmlHelper, ...

                IHtmlHelper Interface (Microsoft.AspNetCore.Mvc.Rendering) | Microsoft Docs[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  Have you imported the tag helpers from your assembly? /Views/_ViewImports.cshtml:

                  @addTagHelper *, YourAssemblyNameHere

                  Managing Tag Helper scope | Tag Helpers in ASP.NET Core | Microsoft Docs[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  J Offline
                  J Offline
                  JimFeng
                  wrote on last edited by
                  #14

                  My assembly is a custom HtmlHelper NOT TagHelper.

                  Richard DeemingR 1 Reply Last reply
                  0
                  • J JimFeng

                    My assembly is a custom HtmlHelper NOT TagHelper.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #15

                    Yes, of course. :doh: You'll want @using statements for your namespace, rather than @addTagHelper statements. Still in the _ViewImports.cshml file, though.

                    @using HtmlHelperExt


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    J 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Yes, of course. :doh: You'll want @using statements for your namespace, rather than @addTagHelper statements. Still in the _ViewImports.cshml file, though.

                      @using HtmlHelperExt


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      J Offline
                      J Offline
                      JimFeng
                      wrote on last edited by
                      #16

                      @using HtmlHelperExt is already in the _ViewImports.cshml, but got the same error.

                      Richard DeemingR 1 Reply Last reply
                      0
                      • J JimFeng

                        @using HtmlHelperExt is already in the _ViewImports.cshml, but got the same error.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #17

                        Did you change the first parameter of the extension method, as I suggested yesterday?


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        J 1 Reply Last reply
                        0
                        • Richard DeemingR Richard Deeming

                          Did you change the first parameter of the extension method, as I suggested yesterday?


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          J Offline
                          J Offline
                          JimFeng
                          wrote on last edited by
                          #18

                          Thank you Rick, after I changed the parm type, and the helper works in my ASP.NET Core 2.1 MVC app.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups