0

I am trying to read content from a json file in .Net core API and assign to a list and use it for doing business logic.

API Layers:

Project -> Business -> Repository -> DB

I need to read content from a json file in the Business layer. File content will not change frequently. I am thinking of reading the file in the Business layer class constructor.

Questions:

  1. Do I need to keep this file in App_Data folder?
  2. Can I use the HttpContextAccessor in the business layer constructor and read the content?

Can someone help in providing the best way to achieve? Please let me know if the question is not clear and I will rephrase it.

  • * 1. It depends on your purpose, if you want to do CRUD operations I suggest you keep the file in the **App_Data** folder otherwise it's not so important. * 2.Yes, you can use the `HttpContextAccessor`. You need to inject it in your class (in the business layer) and then you can use it in your methods – Engincan Veske Dec 30 '20 at 13:39
  • Hi @EngincanVeske, Thanks for your inputs. I am trying to use System.IO without HttpContextAccessor. – Karthick Trichy Chandrasekaran Dec 31 '20 at 12:16

1 Answers1

0

Do I need to keep this file in App_Data folder?

That is entirely up to your intended use of the file. If it is static data or configuration that rarely changes, you could store it with your project and set up your project to copy it to the build folder. This can be done in Visual Studio 2019 by selecting "Always Copy" or "Copy if Newer" as the Copy to Output Directory option., or in the .csproj file directly.

Can I use the HttpContextAccessor in the business layer constructor and read the content?

You can, by registering it with .Add<Transient|Scoped|Singleton>() in your Starup.ConfigureServices. Note that this make it difficult to use this business layer service in a non-request context (e.g. with a scheduled task).

However, if the file is on the same machine as the app, I don't see why you would need the HttpContext to read it. All you need is the System.IO and System.Text.Json namespaces.

Connor Low
  • 5,900
  • 3
  • 31
  • 52