Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Server.MapPath in Business Layer

My business layer creates files and needs to save them in the App_Data folder of my asp.net mvc 4 web frontend.

I could use Server.MapPath in the business layer to get the physical path of the App_Data folder. But i want to avoid a reference to System.Web in the business layer.

Are there other ways to get the path to App_Data in business layer?

like image 424
levis Avatar asked Sep 06 '12 06:09

levis


People also ask

What is the use of server MapPath in asp net?

Because the MapPath method maps a path regardless of whether the specified directories currently exist, you can use the MapPath method to map a path to a physical directory structure, and then pass that path to a component that creates the specified directory or file on the server.

What is server MapPath in MVC?

As you know, Server.MapPath returns the physical file path corresponding to the specified virtual path. If you encounter a Server.MapPath or the visual intellisense doesen't have quick result options, you can fix as below string path = System.

What is server MapPath returns?

MapPath("/") returns C:\Inetpub\wwwroot. Server.


2 Answers

The correct way to deal with this is to have the presentation layer pass the path into the business layer.

To put this another way, the purpose of having a business layer is to create a separation of concerns between the ui and business processes. If you force the business process to know about the ui layer, then you are violating that separation of concerns.

There are a number of ways you could deal with this. You could pass the path into the business layer when the business layer is constructed, such as via constructor initialization or through dependency injection. Or you could pass it to the method call. Or you could create some form of configuration file that your business layer loads that contains the path.

There are lots of ways of going about this that do not violate separation of concerns.

like image 71
Erik Funkenbusch Avatar answered Sep 20 '22 13:09

Erik Funkenbusch


you can use

string  path = System.AppDomain.CurrentDomain.BaseDirectory+"App_Data";
like image 24
Elanchezhian Narayanasamy Avatar answered Sep 23 '22 13:09

Elanchezhian Narayanasamy