16

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
dinotom
  • 4,990
  • 16
  • 71
  • 139

5 Answers5

9

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.

Luaan
  • 62,244
  • 7
  • 97
  • 116
3

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

Community
  • 1
  • 1
Michael Blackburn
  • 3,161
  • 1
  • 25
  • 18
  • As an aside, the Model class needed System.Web.Hosting to throw an async "Fire & Forget" message using `HostingEnvironment.QueueBackgroundWorkItem` on exception that wouldn't get killed by IIS recycling the worker process. – Michael Blackburn Jul 11 '16 at 14:10
1

Do you try with Server.MapPath in System.Web ?

mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • 1
    That doesn't really solve the issue, does it? What if OP needs to use another member of that class/namespace? – user247702 Oct 07 '14 at 13:14
  • @Stijn probably because there is no Hosting class ? – mybirthname Oct 07 '14 at 13:15
  • This is the issue I'm having, I need the QueueBackgroundWorkItem method in the HostingEnvironment class. I can see the namespace, class and method in the object browser, but my code doesn't find it when referencing the 4.5.2 version of System.Web.Hosting, – Michael Blackburn Jul 06 '16 at 20:40
  • @Michael, why not address the 4.5.1 assemblies or skip to 4.6.1. I found targeting 4.5.2 generally problematic. – Eniola Jul 18 '16 at 13:17
  • In my case, Sharepoint 2013 doesn't play nice with 4.6. But I'm definitely feeling the "pinch" of targeting 4.5.2. I was looking at the references in my WebAPI project, but the code that I was looking at was in a different library project that didn't have those references. I've added an answer documenting this. – Michael Blackburn Jul 25 '16 at 18:10
1

I don't think you can use the .NET Client Profile to build Web Applications?

Anyway, that aside. You find that namespaces tend to be re-used across different assemblies. Especially in the framework assemblies where they believe some classes conceptually belong together even if they support different technologies.

I am not sure I explained that well but take this example. There is a System namespace in mscorlib, System, System.Net, System.Core and System.Numerics. Also, System.Web may show up in System.Web.Http, or System.Web itself, and others like System.Web.Abstractions, System.Web.Optimization, etc. As a result just trying to use a using statement to discern the assembly a particular class came from can really throw you off.

The typical classes in the System.Web.Hosting namespace are in the framework assembly System.Web.dll. Microsoft has been trying to de-emphasize the direct use of System.Web.dll in favor of the more modular implementation of Katana/Kestrel.

Having said that, make sure your project directly references System.Web.dll. To use the required class either refer to it by its complete name ie System.Web.Hosting.HostingEnvironment. Or put a using System.Web.Hosting; at the beginning of your .cs file.

It is possible to have a property in the current class that is named HostingEnvironment, or a class from another namespace in another assembly that is named HostingEnvironment. In that instance, you may need to specify the class name in full or come up with a moniker for easy reference and to reduce typing.

For instance, you could have this at the beginning of your file:

using HostEnv = System.Web.Hosting.HostingEnvironment;

Then somewhere in the body of your code, you could make reference to it thus:

var appHost = HostEnv.ApplicationHost;

Does this help?

For the particular scenario you want to address, you may then do this:

var resolvedPath = HostEnv.MapPath(pathToMap);
Eniola
  • 710
  • 1
  • 7
  • 23
-1

Change your library profile from client.

Changing client profile

vborutenko
  • 4,323
  • 5
  • 28
  • 48