How can I use Server.MapPath inside class library project
Asked Answered
K

3

10

I have a web application thet has a number of class library projects. Some example code below.

public static class LenderBL
{
    static string LenderXml { get { return "MyPathHere"; } }

    public static LenderColl GetLenders()
    {
        var serializer = new XmlSerializer(typeof(LenderColl));

        using (XmlReader reader = XmlReader.Create(LenderXml))
        {
            return (LenderColl)serializer.Deserialize(reader);
        }
    }
}

I would normally use Server.MapPath to get the path for the property LenderXml, but when I use it in a class library is returns the path of the parent solution, not that of the class library project.

Is there a way of getting the path for the class libary project itself?

Thanks in advance.

Kennykeno answered 15/4, 2013 at 7:33 Comment(0)
K
31
    var Mappingpath = System.Web.HttpContext.Current.Server.MapPath("pagename.aspx");

Hope its helpful.

Krak answered 15/4, 2013 at 7:37 Comment(2)
The System.Web library will need to be referenced, not only specified.Beadledom
@ Freelancer - Sorry, perhaps I wasn't clear in my question. I can access Server.MapPath in the class library, howver it returns a path to the parent project. I'm looking to get a path to the class library project.Kennykeno
S
13

As I can understand you need the current assembly location

static public string CurrentAssemblyDirectory()
{
    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string path = Uri.UnescapeDataString(uri.Path);
    return Path.GetDirectoryName(path);
}
Sublieutenant answered 29/4, 2014 at 9:28 Comment(1)
This is the only solution I've found that actually works in both a class library and in a web app. Thank youWatercress
C
4

Server.MapPath will always run in the context of the web root. So, in your case the web root is the parent web project. While your class libraries (assemblies) look like separate projects, at runtime, they are all hosted in the web project process. So, there are a few things to consider about resources that are in the class libraries.

First, consider if it makes sense to keep resources in the class library. Maybe, you should have the xml file in the web project and just reference it in the class library. This would be equivilant to how MVC projects keep their views in the web project. However, I assume you can't do this.

Second, you could change the build properties of your xml file. If you change the file to content and copy-always in its properties. The file will copy to the bin directory. Then Server.MapPath should work because the file will be accessible.

Third, you could make the resource an embedded resource and then reference it in code. This is a way to keep all the resources local to a built assembly.

Hope this helps.

Carminecarmita answered 15/4, 2013 at 7:46 Comment(2)
This doesn't answer the question in the slightest.Beadledom
@GrantThomas The OP says that Server.MapPath is working, but returns the path of the parent project. I can only assume the parent project means the web project hosting. These three options are all ways to reference the xml file contents from code in the referenced library. So the way I read it, this is a perfectly reasonable answer.Carminecarmita

© 2022 - 2024 — McMap. All rights reserved.