0

I am calling a helper method in my Services class library project from a controller in my UI web application. I cannot get the proper path to the templates in the services project. I have tried dozens of ways but every time the base path of the full path points to the UI project.

C:\Users\TFD\OneDrive\TestEmal.UI\TestEmal.UI\bin\Debug\netcoreapp2.0\EmailService\EmailTemplates\EmailMaster_Body.html

I am building the path in the Services class library project

private static readonly string ThisDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

This came from the accepted solution from this post, accepted answer by mark Amery

Class library path SO

I have tried every permutation of Path and of Assembly but every one returns the path to the Web UI application.

How do I get the base path of the Services class library project without hardcoding or using Replace?

dinotom
  • 4,990
  • 16
  • 71
  • 139
  • If the service class is intended to pass back the Templates, I would suggest adding a method to it to return the Template as an object instead of a reference to the path that holds it. I think this will give better security and encapsulation. This doesn't directly your question so is just a comment – Dave Apr 04 '18 at 13:52

1 Answers1

0

You cannot. A class library is compiled into the application that references it. After build/publish, the DLL for the library resides in the same directory as all the other DLLs for your application, meaning any paths will always be relative to your web app directory, not your class library directory.

It's not entirely clear what you're trying to achieve here, but if there's simply some file or files in your class library project directory that your class library needs to reference, you need to add them to your project and set them to copy on build in the properties pane for each file in Visual Studio. This will result in the file(s) coming along for the ride and ending up in your web app's build/publish directory as well. Your paths will still be relative to the web app, not the class library.

Alternatively, you can have a build task that does the copy instead, but that's a little more complicated to set up.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • ...I have all the files properties set to copy if newer. They are in a folder in the class library project. By this "you need to add them to your project" did you mean add them instead to the web app project? There has to be a way to get a path to read in a text file. – dinotom Apr 04 '18 at 18:38
  • No. Copying during build.should be enough. Just make sure the relative paths match where they end up in your web app build and you should have no problems accessing them. – Chris Pratt Apr 04 '18 at 18:40
  • ...Okay, then how do you get the relative path in Asp.Net Core 2? Because none of the two dozens ways I've tried has worked. Is Copying during build something different than having them set to copy if newer in properties? – dinotom Apr 04 '18 at 18:42
  • The relative path is the static part. The point is that after getting the path from the executing assembly (your web app) and adding whatever additional static path is required to get to the file from there as well, it should match where the file is actually physically located after build. There's a disconnect somewhere. Look at where it's trying to find the file, look at where the file *actually* is after build, and then fix your code accordingly. – Chris Pratt Apr 04 '18 at 18:47
  • ...all set my original code worked. I checked the built folder and, of course, the one template I was testing with wasn't in there. Of course, because it was the only one that the copy property wasn't changed from copy never. Thanks for the extra eyes – dinotom Apr 04 '18 at 18:58