1

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.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
  • 3
    That looks right to me... it looks like there's a *solution* folder `WebDemo`, and inside that there is a *project* folder also called `WebDemo`. Have you double-checked in explorer to see if it is right? i.e. does `C:\Users\tom\source\repos\WebDemo\WebDemo\StudentLib\Reports` exist and have your file in it? – Marc Gravell Feb 05 '19 at 14:29
  • 2
    note: libraries don't resolve from the library location - when you build, libraries are typically *copied* into the build output, with the application running purely from there; the fact that the library has a different path is irrelevant - that isn't where it comes from at runtime – Marc Gravell Feb 05 '19 at 14:33
  • `MapPath` is "I'll give you the extra sections you'd add to the URL for this web application's root, you tell me what physical path corresponds to that". But you're trying to supply a "URL" which doesn't refer to anything *within* the web application. – Damien_The_Unbeliever Feb 05 '19 at 14:41
  • @MarcGravell , Yes you are right `C:\Users\tom\source\repos\WebDemo\WebDemo\StudentLib\Reports` exists. So is there a way to get the absolute path of the file inside `Class Library Project` without using `Server.MapPath` ? Or should I move the `StudentReport.rdlc` file Under `Web API Project` ? – Shaiju T Feb 05 '19 at 15:07
  • @Damien_The_Unbeliever , So how do I get the path of `StudentReport.rdlc` file from a `Class Library Project` ? – Shaiju T Feb 05 '19 at 15:10
  • 2
    @stom where it resides inside the class library project is frankly irrelevant; it isn't the class library that is running - it is the web application that runs, and the web application has a *copy* of what it needs from the class library. If you need to resolve the RDLC from the web app, the *easiest* way to do that is indeed to have it as content *in the web app* – Marc Gravell Feb 05 '19 at 15:10
  • 2
    If it "belongs" in the class library and should be available wherever the class library is used, you might be looking for embedded resources. But then, at the point of usage, it no longer *has* a physical path. – Damien_The_Unbeliever Feb 05 '19 at 15:12
  • @MarcGravell ,Thanks, Last question, Is it best practice to have `SSRS` `RDLC` files and `DataSets` Under `Web API project` ? – Shaiju T Feb 05 '19 at 15:17
  • 1
    @stom that depends on where it notionally "belongs"; as Damien notes - you can use embedded resources, but the API for accessing those is quite different (it isn't a file, but you can still get a `Stream` for it) – Marc Gravell Feb 05 '19 at 15:20
  • @MarcGravell , Actually the requirement was to render `SSRS RDLC` report `PDF` as `bytes` to clients consuming the `API`, I am planning to move the `RDLC` files under `Web API project`. Thank you. – Shaiju T Feb 05 '19 at 15:29

0 Answers0