Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Add as Link" for JavaScript files returning 404 in debug

Using a Visual Studio 2010 ASP.net web application, I have several projects that share some JavaScript/css files. The most logical way for them to share these files is to place the files in a single folder and each project has them included with the "Add as Link" option. However, if I add the files this way when I'm debugging using either the Visual Studio Development server or debugging using a local IIS web server all requests for these files return 404 Not Found errors. If I publish the site then the files are copied but that obviously doesn't help with debugging.

Is there something I'm missing or is this a failing on VS's part?

like image 969
Matt Avatar asked Nov 12 '10 16:11

Matt


3 Answers

To overcome this problem some time ago I created a 'MSBuild.WebApplication.CopyContentLinkedFiles' nuget package. This package adds MsBuild target which copies all content files added as link to project folder during build.

Note: if you use source control then it is better to add copied files (from Web Application folder) to ignore list.

like image 80
Maxim Kornilov Avatar answered Nov 08 '22 20:11

Maxim Kornilov


I wouldn't really call that a failing, since you asked for that behavior in the first place: linked items in Visual Studio projects are actual links to external files. Those files can reside anywhere on the disk and are not copied into the project folder.

You might want to copy those files locally yourself during a pre-build event. That way, the files will remain synchronized and you won't duplicate them until your first compile.

like image 5
Frédéric Hamidi Avatar answered Nov 08 '22 20:11

Frédéric Hamidi


The problem seems to be that the website runs right from your source folders, rather than from the bin folder. This means that the file will be missing, whether or not it is copied to the output folder.

It's probable that running from a local or remote web server would not have this problem, though I didn't get that working, and I'd rather not add IIS to my local machine if I don't have to.

Adding a pre-build copy command did work. Note that the current directory will be the bin folder. (You can use cd to echo the current directory to the build window if you want to see it):

If the file is in another solution, your command will look something like (three ..s: one to get out of each of bin, project, and solution folders):

copy ..\..\..\OtherSolution\OtherProject\Scripts\MyJSFile.js ..\Scripts\

If it's in the same solution, but a different project:

copy ..\..\OtherProject\Scripts\MyJSFile.js ..\Scripts

One minor issue is that the link to the file will collide with the new copy of the file, even if you don't add it to your project. As long as you make the link first, it seems to work. If you copied the file first, you'll have to manually delete the copy, and then refresh the solution explorer before before being able to add the link.

like image 1
Dave Cousineau Avatar answered Nov 08 '22 19:11

Dave Cousineau