1

In one of my class libraries, I wish to only make a single call to System.Web.Mvc.Server.MapPath(string path).

If I install the NuGet package Microsoft.AspNet.Mvc into that class library, I have to first install the Microsoft.AspNet.WebPages package as well, which may have further dependencies.

I just want the single DLL System.Web.Mvc.dll since the Server class is in there.

I tried referencing the DLL from the path:

C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45

but that DLL does not have the Server class in the System.Web.Mvc namespace.

And I do not see this DLL anywhere else on my machine.

Where can I reference this DLL from? It used to be so easy in the old days. And it stayed easy if you wanted to reference just MVC till 2010.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • 2
    You don't need System.Web.Mvc for this, see here http://stackoverflow.com/questions/1199486/server-mappath-in-c-sharp-classlibrary – Esko Jul 06 '16 at 12:46
  • [HttpServerUtility.MapPath](https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx) is a part of `System.Web` (both namespace and assemby). It is not specific to MVC. Also a more robust solution could be using [HostingEnvironment.MapPath](https://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.mappath.aspx) as that is not web server specific. Also see previous question on the difference between the 2 on SO here: http://stackoverflow.com/a/3796042/1260204 – Igor Jul 06 '16 at 12:47
  • @Esko Thank you. Please write that as an answer? – Water Cooler v2 Jul 06 '16 at 12:49
  • @Igor It is there is both the assemblies -- System.Web.dll and System.Web.Mvc.dll – Water Cooler v2 Jul 06 '16 at 12:50

2 Answers2

1

There is no need to have System.Web.Mvc-assembly in your project to use Server.Mappath. All you need is reference to System.Web in your project and you can do

var path = System.Web.HttpContext.Current.Server.MapPath("default.aspx");
Esko
  • 4,109
  • 2
  • 22
  • 37
0

If I install the NuGet package Microsoft.AspNet.Mvc into that class library, I have to first install the Microsoft.AspNet.WebPages package as well, which may have further dependencies.

Yes, and that hasn't changed since 2010. But NuGet made it much easier by installing both the files to reference and adding the references to those files in your project and adding any dependent NuGet packages in one easy step.

Where can I reference this DLL from?

The files that are installed by NuGet are physically located under your root directory (the same place where your solution file is) in a directory named \packages.

But NuGet automatically adds the references to the files for you when you install the NuGet package, so there is no need to mess with them unless something goes wrong.

For example, to install Microsoft.AspNet.Mvc, you simply need to run the following command in the Package Manager Console.

PM> Install-Package Microsoft.AspNet.Mvc -Version 5.2.3 

Which will install version 5.2.3 of MVC along with all of its dependencies and reference those DLLs from your .csproj file as well.

Further reading: Using NuGet Part 1: NuGet Basics

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • No, NuGet did not exist in 2010. Secondly, I did not at all ask any of the basics about NuGet, with which I am familiar, in my question. – Water Cooler v2 Jul 06 '16 at 13:03
  • I didn't say that NuGet existed in 2010, but its introduction did make the concept of referencing DLLs and their dependencies easier. I am not sure I understand what your question is, since this seems to be common knowledge if you are familiar with NuGet. Dependencies are installed by NuGet *into your project* and it also adds project references to them automatically, so to ask where they are seems to indicate you are trying to do something manually that NuGet already does automatically. Most dependencies are no longer referenced from a common location at the system level. – NightOwl888 Jul 06 '16 at 14:52