I am migrating a web application from VB to C#. I have also upgraded to Update 3 in VS2013. Were there changes to the Hosting
class? I'm getting an error using Hosting.HostingEnvironment.MapPath
and I can't even add a reference to System.Web.Hosting
as it's nowhere to be found. When I try to search the assemblies when adding a reference, using the whole namespace i.e. System.Web.Hosting
, it returns no result.
I have the using statement in the class and it is NOT grayed out meaning it is being used for something but yet the code doesn't like Hosting
in Hosting.HostingEnvironment
as it's in glaring red. I don't even get the Hosting
class in intellisense and the project has a reference to System.Web
1 right click on References in Solution Explorer and press add reference... 2 choose the browse tab and go to C:\Windows\assembly\GAC_32\System. Web\System. Web.
The hosting environment in ASP.NET Core is used to indicate at runtime on which environment (Development, Staging, or Production) an ASP.NET Core application is running.
There is no Hosting
class. Instead, you want the HostingEnvironment
class:
HostingEnvironment.MapPath("~/Hello.txt");
The full type of HostingEnvironment
is System.Web.Hosting.HostingEnvironment
, so you need to have a using System.Web.Hosting;
clause in the file, or use the full name.
Even more importantly, though, if you're making a web application, you most likely don't want to use HostingEnvironment
anyway. You should always have an instance of e.g. HttpContext
or Page
/ Control
, which give you access to Server.MapPath
, which should be preferred.
As for the reference, System.Web.Hosting
namespace lives in System.Web.dll
, so just make sure you have a reference to that and you should be fine.
Since you're migrating this from VB, I assume that the conflict is caused by VB's different treatment of namespaces. In C#, you can't just do this:
using System.Web;
Hosting.HostingEnvironment.DoWhatever();
When using a namespace, either use the full type name including the namespace, or use a using on the exact namespace, and the type. Combining the two doesn't quite work.
I had this same issue, was something other than what is posted. I was looking at code in a library in a separate (Model) project. In the library project I was using, System.Web wasn't referenced.
The tricky part was that System.Web.Http
was referenced, so the System.Web namespace was found, so the statement using System.Web;
compiled fine.
Save yourself some trouble and heartache, always press the "Sync with Active Document" button in the Solution Explorer, as depicted in this answer. https://stackoverflow.com/a/30517179/149884
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With