Why Server.MapPath is adding extra directory in the path

526 views Asked by At

I am using HttpContext.Current.Server.MapPath function to locate a StudentReport.rdlc file which is under a Class Library Project.

Solution Structure:

enter image description here

I am calling below function which is in Class Library Project from a Web API Project named as WebDemo.

public class Reports
{
    public string GetReportPath()
    {

        string path1 = System.AppDomain.CurrentDomain.BaseDirectory;

        var path2 = System.Web.HttpContext.Current.Server.MapPath("~/StudentReport.rdlc");

        var path3 = System.Web.HttpContext.Current.Server.MapPath("~/BL/ClassLib/StudentLib
                                                /Reports/StudentReport.rdlc");

        var path4 = System.Web.HttpContext.Current.Server.MapPath("~/StudentLib/Reports
                                                /StudentReport.rdlc");

        string path5 = System.Web.Hosting.HostingEnvironment.MapPath("~/StudentLib/Reports
                                                /StudentReport.rdlc");

        return path5;
    }
}

As you can see, I have tried all combinations of Server.MapPath but still it adds etc WebDemo directory in all the paths.

Current Path:

"C:\\Users\\tom\\source\\repos\\WebDemo\\WebDemo\\StudentLib\\Reports\\StudentReport.rdlc"

Expected Path:

"C:\\Users\\tom\\source\\repos\\WebDemo\\StudentLib\\Reports\\StudentReport.rdlc"

May I know why this is happening ?

Any help would be great.

0

There are 0 answers