Issues with Server.Mappath

Posted on 2007-07-30
Last Modified: 2008-02-20

I am trying to use Server.Mappath in a Class library ( DLL Developed in C#).
After some research i found that i should be using the following command to get the Server path from the DLL.


I am using the following code

using System.Web;
using System.Web.UI;

String Filename;
Filename = System.Web.HttpContext.Current.Server.MapPath + "test.xml";

but I get this exception.
::Object reference not set to an instance of an object.

Your help will be appreciated.
Question by:Star79
    LVL 17

    Accepted Solution

    A class would probably need to have the httpContext sent into it from the calling web page.

    I suggest possibly adding a property of HttpContext type to pass it and then doing your mappath.
    LVL 8

    Expert Comment

    Filename = System.Web.HttpContext.Current.Server.MapPath("test.xml");

    try like this

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video discusses moving either the default database or any database to a new volume.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now