-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Can't resolve IHttpContextAccessor, IActionContextAccessor, IUrlHelper in RC2 #3936
Comments
Is this the proper way?
And I think for IUrlHelper, I have to create the instance as |
Why would you need those services in you controller ? The http context and action context are available. |
Oh, sorry. Actually I don't need them in my controller. I was just testing them there. I have a service which needs |
@evorine can you show how you're trying to get the services? Are they constructor parameters of your tag helper? |
We had to make |
Ah indeed, need to depend on Closing this for now. If you can show some code that you think should work but doesn't, please let us know. |
Hi, I have the same problem. I would like to get access to Same for |
You need to manually register these in your startup code.
|
ok great, thank you! |
I require IUrlHelper mostly for the
I've created my own object with this copied logic to inject since it's much lighter so am wondering if maybe even a static method could be created on UrlHelper to contain this logic so we don't have to duplicate it? like:
which could then just be re-used by the non-static |
@Shazwazza at this time that's not a method we're looking to introduce because we're concerned it might add more confusion around which method to call and what to pass in, and I'm not sure it's a particularly common scenario. In the meantime I recommend just having that code in your app (it's only a few lines anyway). |
@Eilon no worries, i'll just keep the code in my app, thanks ! :) |
@Eilon @rynowak @davidfowl Think it would be a good idea to create an announcement on this (the removal of |
When I create a new project from "Web API" template, I can not inject |
Thanks! |
😁👍 |
So, I answer my question: It's registered by Identity (aspnet/Hosting#793 (comment)). |
@CGollhardt Because it's resolved as a singleton and assigned the Also, BTW, in 2.1, there's a helper method, |
I googled the error that comes up and nothing came up, but found this thread via other means. I'm including the original error so hopefully someone else Googling for this finds the answer:
I originally had this line: services.TryAddScoped<IHttpContextAccessor, HttpContextAccessor>(); I replaced it with: services.AddHttpContextAccessor(); And that resolved the issue. I'm going to open an issue on the 2.0 -> 2.1 migration page, this should be in the documentation. (Issue now opened here.) |
Hello,
I'm currently using the latest RC2 version (build 16854).
I can't inject some MVC special services (
IHttpContextAccessor
,IActionContextAccessor
,IUrlHelper
) into my controllers.I searched but could not find anything about this. Did I misconfigure something? Or if I have to add them as services manually how can I do it?
The text was updated successfully, but these errors were encountered: