1

Doing a project between multiple people, and a few components (web app, services app and some others). We will be storing some information inside the Content folder of the web app so it can be accessed directly from the web server with an href, however other components outside of the web app need to access this folder as well, and since we are sharing the project between multiple people using an absolute path is not an option. What options do we have?

EDIT: Trying to explain it a little better.

What i have exactly is, a web project, a "data project" which is just a dll, a "logic" project which is another dll and a services project which is an exe/service. Both the web project and service project consumes the methods from the logic, and the logic from the data project. Being the last one the responsable for storing data (in a database) and also in the file system. This "filesystem" path should be configurable, and we are aiming to put it into the content folder of the web project so multimedia files can be accessed directly rather than doing a byte stream.

Now in the web.config(config file of the web app), and app.config(config file of the services app) i could set the absolute path to web/content (the same for both config files) and the data dll would use it without problems. Now the main problem is that we cannot put an absolute path in the config file because each person works on a different computer with obviously different file paths. So if i could just write something like: ~/project/Web/Content rather than C:/myfolder/stuff/blabla/project/web/content in the config files, with ~ resolving the path to the project, this is what i want! Or maybe better ideas about how to share a folder with these apps without adding absolute paths hardcoded somewhere.

Cristiano Coelho
  • 1,675
  • 4
  • 27
  • 50
  • We need to access files/directories inside the code, which are inside a specific folder inside the web project. And using an absolute path wouldn't work. – Cristiano Coelho May 27 '13 at 06:09
  • Can you add some sample code about what you really want to do in your project? – Thai Anh Duc May 27 '13 at 07:37
  • It's a little bit hard to show code, but basically, i have a web project and a dll project responsable for manipulating data. This dll needs a path to where to find/store data, and this dll will be called from the web app and also from other services apps so it needs to be an absolute path. However since we are a few people working on the project, we cant really use an absolute path for everyone. – Cristiano Coelho May 27 '13 at 21:24
  • In term of design, that dll should not have reference to Request or Server object. And actually, all it needs is: How to resolve a path or where is the root folder. Thus, i would make an Interface: IResolvePath { string Resolve() }. Then for each Web project/ services, you can supply the correct implementation. – Thai Anh Duc May 28 '13 at 01:48
  • I dont think that would accomplish what im looking for. Read my edit i tried to explain it better. – Cristiano Coelho May 28 '13 at 06:34

2 Answers2

2

What you want to use is:

Server.MapPath("/Content/filepath.ext");

This will give you the absolute path of a file based on it's position within the website, in this case, from the /Content directory.


For a program external to the website, you have a couple options;

The easiest to implement might be a simple configuration value in the external program which points to the directory. My guess is you've already decided that's not ideal, but it may be the quickest way.

Alternatively, there's a Microsoft .NET assembly which gives you easy access to IIS information (I can't recall its name off the top of my head!). You could use this assembly to find the appropriate website, and retrieve its root directory. I'll see if I can find it and get an example, or maybe someone else will see this and post an answer with that information.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • This would work from within the web server, but what about an external project (ie a services project) which also needs this path? Querying the web server for the path isnt really an option. – Cristiano Coelho May 27 '13 at 17:52
  • Ideally it would be a value in a config file pointing to the absolute path, however while developing each one of us should change that value and never commit it. Not sure if svn supports something for "not commiting a line" – Cristiano Coelho May 27 '13 at 18:17
0

Please check the following method "ResolveClientUrl" MSDN Use the ResolveClientUrl method to return a URL string suitable for use by the client to access resources on the Web server, such as image files, links to additional pages, and so on.

http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveclienturl.aspx

Taimur Khan
  • 531
  • 8
  • 21