"Add as Link" for JavaScript files returning 404 in debug - asp.net

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?

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.

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.

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.

Select the link in Solution Explorer and then look at properties window and set Copy To Output Directory to Copy Always. Linked items are set to Do Not Copy by default.
BTW, you can copy many files as links very easily directly from Solution Explorer when using VSCommands 2010 extension.

See this blog post about a simple addition to your project file.
http://mattperdeck.com/post/Copying-linked-content-files-at-each-build-using-MSBuild.aspx

Related

Cannot rebuild ASP.NET Web Applicaiton missing solution file

I am taking over an ASP.NET Web Application using C# and all the code (including C# source) was given to me without the solution file (something.sln) so whenever opening the Web Application in Visual Studio 2017 I have to open by "Folder..." instead of by "Project/Solution...". Once open, I am able to make the necessary changes to the code behind files such as Default.aspx.cs or somethingElse.aspx.cs, but when I try to test the code changes by going to Run > Start Without Debugging, I couldn't find the Run menu option; I see the Build Solution button but it's disabled (greyed out).
Every time I try navigating directly to my site at http://dev.mydomain.com. The code changes I made did not get compiled neither.
So how do I get the Run or Build menu option enabled?
The easiest way to correct this issue is probably to create a new, empty ASP.NET WebForms project (and a new solution file as well if necessary) and copy your code files into it. You can then tell Visual Studio to "include" those files in the project.
You may (or may not) need to make the odd other minor adjustment too but those basic steps should get you started.
Not sure if this is related, but adding the sln file to the git ignore removed its visibility from VisualStudio. I had to open the project csproj file in VisualStudio.
THen on close, the prompt was asking me to save a new SLN file with the same solution name i was looking for. I was then able to go to the directory with the original solution in it, and then I was able to open it again in Visual Studio.
For all those who come to this post in the future, I would check your gitignore files and see if they ignored the sln file.

Is it a bad idea for my solution and project to be stored in separate locations?

Somehow my solution and project differ as to their Path/FullPath.
The Path property of my solution ("customerreportingnet") is:
C:\Users\cshannon\Documents\Visual Studio 2013\Projects\customerreportingnet\customerreportingnet.sln
The only project beneath that solution is a Website ("http://localhost/EMS/customerreportingnet/").
The website project's FullPath is this:
C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet
Why would the solution and project location differ?
Did I do something wrong in the setup? I downloaded the files from a .zip file, and extracted them to C:\EnhancedMonthlySalesReporting\
I then created a Virtual Directory in IIS, mapped it to C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet, giving it the Alias "EMS".
Then in VS I open the website (solution/project) this way:
File > Open Website...
and then I open IIS > EMS
When I do this, I do not see any files beneath the "project" - all that's visible in the Solution Explorer are just the solution and project names - no subfolders or files beneath them. But when I right-click the solution "customerreportingnet" and select "Open Web Site", then reply OK to "Open the Web site (this will close the current solution)" all is relatively well - I can right-click the project ("http://localhost/EMS/customerreportingnet/"), select View in Browser (Internet Explorer) and the site runs.
The only files in C:\Users\cshannon\Documents\Visual Studio 2013\Projects\customerreportingnet\ are:
customerreportingnet.sln
customerreportingnet.v12.suo
C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet has those files and many more (as well as beaucoup folders).
Should I try to reset the solution Path to C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet, or is that just an oddity, but not a problem?
If so, is that as easy as just changing that property value, or will that mess things up?
Is it a bad idea for my solution and project to be stored in separate
locations?
A solution is a "container" for projects. Where they are in your local file system doesn't really matter. So in that context, it's "fine".
It's not unusual to have some solution context that is composed of some "projects you did before". So you can "organize" a "new" solution composed of projects located elsewhere (from some other "solution") along with new items.
However, once you get into Source Control, whether it's TFS or GIT, then it will matter - they (projects in a solution) have to be in the same parent folder to be handled easily. So in that context, it's "bad".
TLDR; there are still ways to effectively source control "projects in different folders" - each one is it's own separate "repository". But you'll have to manually/separately do your syncing, merging, etc.
If I'm following your post correctly, I think what you're looking for is Add..., not Open... - you want to add an existing project or web site to a Solution.
Depending on what you're after, and what files you're dealing with:
if you have a Project in the zipped file, then you can Add Existing Project, and so on...
Unsure why you needed to do what you did in IIS - you can do debugging within Visual Studio (IIS Express).
Alternatively, you can Publish your web site/application from Visual Studio and target a local file system folder that you can then set in IIS (Local IIS if installed) - this way, you sort of have a "dev" (VS) and "staging" (publish folder mapped in IIS) environment in your local machine.
Hth...
After you extracted your code and opened the project in visual studio.
Visual studio will automatically create a solution file if you open a project (you can also open a solution file directly). Once you click save all or save your solution file it will save it to the previous location or to the default location.
Since you did not save the file before, visual studio will save it to the default location:
C:\Users\cshannon\Documents\Visual Studio 2013\Projects\[ProjName]\[ProjName].sln
If you want other developers to be easily able to open your project. (especially when you start to have multiple projects inside 1 solution), you want to include the solution file inside the project location. Otherwise every developer has to create this solution file himself, which is annoying and cumbersome work.
Since dotnet core the best practice file structure is this:
/root
/src <== contians all the projects per folder
/project1
/project1.csproj
/etc*
/project2
/[ANameForSolution].sln (so directly inside the proj folder, next to src)
The old habit people use was more flat:
/root
/project1
/project2
/YourSolution.sln
The best practice, moving everything to /src is because lately a root of the project already has a lot of (config) files, and this way you can keep the root a bit cleaner.

Exporting entire project in Visual Studio 15

I need to send off this project I've been working on via email so I'd like to just zip up the project folder and send it off. I've done this in previous version of VS with no problem but with Visual studio 15 the project folder only contains the solution file a .vs folder and a packages folder. None of the actual files are included i.e default.aspx and so on.. If I try to zip up this folder and send it off it doesn't open on the other end. I've tried exporting as a template but that didn't really work because it doesn't contain the solution file. Is there any easy way to just export the entire project so that it can be easily opened by another user on a different computer (They need to be able to see each individual file in the solution basically how you see it in the solution explorer)?
I use CleanProject for this, amazingly, it still does a fantastic job with my VS2015 projects. It cleans (removes bin/, obj/ etc) and then creates a minimum zipfile.
You can download it here:
https://visualstudiogallery.msdn.microsoft.com/bca632e0-3948-43c8-b337-da47275717b6
It does not work from VS2015, but from the file explorer. If that doesen't freak you out, it's a great timesaver for those situations :)

Is it possible to get app_offline to be ignored when running in visual studio?

I have a web app that has an app_offline.htm file, this file is stored in source control along with everything else.
The problem I have is that each time I (or any other developer) do a get latest on the source I get the app_offline.htm file and then when I try to run the app all I see is the app_offline rather than the actual web site.
Is there way that I can stop this from happening?
You can right click on the file and choose exclude from project. It renames the file so you IIS won't pick it up, but I'm pretty sure that Visual Studio will not remember that after you do a new checkout. (Can't verify this though)
Other than renaming it or removing it locally, it most certainly depends on your source control system. The whole point of source control is to "restore" a code source to a specific point. Checking out something other than the current state is kind of against the source controls principle..
Can you perhaps rename the file in source control and when you publish it you rename it to app_offline.htm then instead?

How to update an asp.net website (just changed files)

I am developing an asp.net website that will need regular updates for source codes and HTML sides. After i have completed the necessary updates, i use the 'publish website tool' and publish the site to my local directory. Then i upload all files to Remote File. Is there any way to fix my site with just changed files. For example if i have updated just 2 files of 84 (HTML or source side), is it possible to update just this 2 files without any problem?
While Visual Studio does offer a few different compiling/build options, I think that you are probably doing everything just fine for a Visual Studio Website project. When I'm working with a website rather than a web application, I will only FTP the files that have changed. For example, if I were to change some HTML tags around within an .aspx page, then only that page would need to be uploaded to the web server. If I change the Page_Load function in the .aspx.cs page, then I will definitely need to get the updated DLL on the web server as well.
I use FileZilla for my FTP tool and there is an option to only upload the changed files. So after you use visual studio to publish your website, you can grab the entire contents of that directory and drag it over to the FTP server location and only update what has changed.
The ASP.NET website project offers an option in the Project properties -> "MSBuild Options" tabpage where you can set "Allow this precompiled site to be updatable".
Setting this option
"Specifies that the content of .aspx
pages are not compiled into an
assembly; instead, the markup is left
as-is, allowing you to change HTML and
client-side functionality after
precompiling the Web site. Selecting
this check box is equivalent to adding
the -u option to the
aspnet_compiler.exe command."
according to the docs.
I've been entirely underwhelmed by the available tools to VStudio. And am instead using Gulp.
Even in 2017 the problem remains.
With gulp you can "watch" directories for changed files, so I have it configured to watch the [bin] folder, along with assets separate from aspx/cshtml files.
That way whenever I change anything, it is instantly copied to my publish folder where I can later zip it up and deploy ONLY the changeset. The script even has a delay so I can delay the copy/upload in situations like bundles where they are generated dynamically and take some time to be fully modified.
The only thing I have to do is clean it up before I start my next milestone, so that the "publish" folder which gulp "auto deploys" to is empty and ready for the next run. Emptying a folder and running "gulp watch" in background seems like a small price to pay for such a needed feature.
I feel like this should be a good item to develop as a vstudio plugin.
Recently I have been forced to move to a windows/C#/.NET/MVC environment from linux/node/angular. Sigh. I found the following solution (next paragraph) elminates the pain caused when using visual studio to "publish" the code. The VS2017 publish process copies every file in the entire application to the web server, even if just one character is changed in one file. This can take over an hour for our moderately sized app.
So here's what I do. I first publish the solution locally (typically to bin/release/Publish/). That takes about 1 minute as opposed to 1 hour to publish to the server. Then, I compare the files between my local Publish directory and the server directory using FreeFileSync. FreeFileSync is amazing -- and free. I have access to the server directory via a windows file share. The compare takes about 15 minutes. I can then see exactly what files are different and need to be pushed. Note, the option I use compares the actual CONTENT of the files, not just the create time. I then use the FreeFileSync sync feature (mirror option) to copy the few files needed to the server. This takes maybe two minutes. So the total operation takes about 20% the time Visual Studio "Publish" takes. But best of all, the actual hit on the prod server is only the two minutes it takes to copy the diff files, rather than the 1 hour outage inflicted by Visual Studio Publish as it slogs along copying each and every file.
It depends on the type of project.
If is a project created with File > New Web Site then is ok to just copy the changed .aspx files (make sure that the corresponding code - .cs - files are also copied).
If the project was created with File > New Project > ASP.NET Web Application then you will need to copy the .aspx files and the compiled project dll (by default the dll has the same name as the project like TestProject.dll) from the bin folder inside the project.
i think you need something http://winmerge.org/
where it will compare files that are changed and upload them.
hope this helps

Resources