views:

53

answers:

3

How can I get the path of a cs file in aspx?

Here's the issue: I've created a property class in a property.cs file which is used in an EPiServer admin module. Server.MapPath() will hence return the path of the executing file, which is in a totally different place than my code. I need to get the path of the property.cs file (from inside the property.cs file) in order to dynamically set some relative paths to css and js files. How can this be done?

I hence want to be able to include .js and .css files in this cs file, all files located in the same directory, but the cs file is accessed from the EPiServer UI.

A: 

ASP.NET is compiled, you should never need to read settings from source files at runtime, you should read from configuration files (*.config), if they need to be dynamic, these too can be injected during the page lifecycle via a variety of methods.

RandomNoob
True, but I'm making a reusable piece of code here. Since it's a property (in EPiServer) that will have its own UI, and I want the users to be able to include it without having to edit paths and such, I figured it would be easiest if the js and css file would be in the same directory as the code that defines the property and that the code can get all it needs from that very same dir.
Pedery
+2  A: 

I would highly recommend not doing what you are trying to do. You'll be constructing a brittle dependency on files that should not even by deployed with your project.

If you have web classes that rely on resources like javascript and css, you should use the ClientScriptManager (or ScriptManager for ajax apps) to register the script files onto the page, and the scripts and css should be deployed into their own regular web directory.

If deployment location is a problem, and you're creating some kind of reusable, redistributable module, then I would recommend that you embed the .js and .css files as WebResources in your assembly, and use the script manager to register the scripts to the page that way, with ClientScriptManager.RegisterScriptResource().

womp
I appreciate the answer, but I think you've misunderstood me. Actually I'd be more than happy to register the script files like you say. The problem is the *path*. How can I know the path to script.js and mycss.css in the same dir as myproperty.cs from coding inside myproperty.cs when this file is called from an EPiServer assembly and MapPath() hence will return the aspx file that is currently calling code in myproperty.cs?I've currently (temporarily) solved this issue by setting the few lines of css in a LitteralControl and adding it to Page.Header.
Pedery
My point is that your .js and .css files should not have a path dependency with your .cs files. They should either be deployed into a well known directory, or embedded with the assembly.
womp
I understand. And this would be sweet in most cases. But if you're familiar with EPiServer that means that setting the filepaths had to be done every time the property was copied and used in a new project. This can be likely brittle if someone changes the directory structure and such a path is hard coded in the source. I *wanted* the path to be relative to the file where it was used so that I *could* include the files properly, but from my understanding this can't be easily done...
Pedery
A: 

I think you should go with including your CSS, JS and other static files as embedded resources.

That will include the files inside the DLL, which makes deployment easier. You can then set up a HTTP handler which will serve you the contents of the embedded files - or use the aforementioned RegisterScriptResource() method.

By embedded the files you don't have to know any file paths.

Ted Nyberg
Yep, I considered that. But I also want to give the third party developers the opportunity to change the CSS or images to suit their design. An option could be to include *both* the source and the resource file, but then again I'd be adding complexity where I think it shouldn't be needed. In any case I ended up going for a solution where the programmers need to change the paths of the links to the JS and the image generator file and aside from that the code configures itself.
Pedery