Reputation: 698
I have a Javascript source file that is common to different projects, which I want to move out of application specific directories and move into a common spot which all my projects can reference.
However I can't seem to reference the file in the <script>
tag of my Views. I've tried using the ..
symbol to escape up the directory chain but the HTML page doesn't seem to recognize the resource. Which would make sense because how would the HTML page know about a directory above the root of the application? Speaking of the root, I tried using ~
to indicate the application root and then I got the following error:
Cannot use a leading .. to exit above the top directory
Is it simply not possible to link to outside resources?
EDIT: After adding the ASP.NET v4.0
app pool to the access control list for the .js
file, the GET
request succeeded. However, in my controller action where I am grabbing the file, I am going through the virtual directory I had previously created. If this is the case, shouldn't I just be able to source the file using the virtual directory instead of my controller action? When I try to do this, the GET
request fails again. When I try to navigate to this source manually, I get an Internal Server Error 500.19
. It seems to be looking for a nonexistent web.config
file.
Upvotes: 0
Views: 2254
Reputation: 698
I finally managed to solve this problem, I ended up duping some files anyway but I am happy with the result since I only have to make changes to one file (at the library location) and it gets propagated to the rest of my projects.
I set up a folder in the general Scripts
folder (which we'll call lib
). Within lib
I added the library .js
files as links, so they are pointing to the library location. This way I can edit the files directly within my project, but the changes are really happening at the library location. To make sure that the files still get included in the project when it is deployed, I added a pre-build batch script which copies the files to the folder with xcopy
. This way, the changes can be made from the file directly or within my project, but the up to date files are always copied over before a build. This solves the double-dot issue of escaping out of the root directory as well.
Upvotes: 0
Reputation: 218828
Is it simply not possible to link to outside resources?
Not directly, no. This has nothing to do with ASP.NET or MVC or anything like that. It's simply a matter of whether or not the client can access the server-side file. If that file is outside of the web server's root, it can't be accessed. (Take a moment to imagine a world where any file on your server could be downloaded by any client who could specify the path to it. And then realize that things like that have happened.)
Any file that's not directly accessible by clients would need to be served by the web application in some way. This is a common approach for serving image files from a database, for example. You might have a single entry point page, something like:
/Scripts/Script/123
Which your markup would reference:
<script type="text/javascript" src="/Scripts/Script/123"></script>
Then in your server-side code for that action you would capture the identifier for the script being referenced (doesn't have to be an integer, I'm just using that as an example, you just want to be able to identify a file in some way), read the file from the disk, and write the file to the client. Which could be something as simple as:
return File("path/to/file.js", "application/javascript");
The browser doesn't care if it's a "file" or if it's being fed as a string to the output. This is because HTTP doesn't have "files". As long as the response body has the right content and the headers specify the right kind of content, it's no different from downloading the "file" directly.
Upvotes: 3