Change default Webdev.Webserver Physical Path on a VS2008 project - asp.net-mvc

I think I've seen this done once, but I cannot find it again after a long Googling/Binging session!
I'd like to create a new ASP.NET MVC project, then create a folder "Web" inside the project, and move all the "web-related" files to inside this folder.
The problem is that when creating this folder structure, I cannot simply press "F5" to start the Webdev.Webserver, because the "physical path" of my application is still pointing to my project root folder.
In the project settings, I've found the configurations for the development server, where I can change the port, for example, but there's no option to specify the "physical path".
Is there a way to do this?
Thanks!

If you are trying to separate Web-related files from, say, Domain-related files, the best you have to do is to created separate projects inside the same solution.
Let's say you create a Store solution. You could then create different projects called Store.Web, Store.Domain, Store.Test, etc.

Related

relocated MVC solution folder in ASP.NET

When I create a MVC project in Visual Studio, it will automatically generate a solution folder with the same name alone with .sln file, packages folder.
For example:
/MyProject/MyProject
and when I run the application in server:
http://XXXXX/MyProject/MyProject
I hate the address. I want to the address looks like:
http://XXXXX/MyProject
I tried to move the solution folder to upper level directory, however, the MVC project can still run but show errors and many warnings.
Is there any way I can create a MVC and I can specify the solution folder? or I can relocate the MVC solution folder nicely?
Thanks
You can relocate sln file.
Just close solution, move sln file to project folder, and then open sln file in VS.
Project will not load. Delete unloaded project in Solution Explorer, then right click on solution (in Solution Explorer) Add => existing project...
and select .csproj file of the project.
Rebuild application, nuget will restore packeges.
But I don't know why You hate path.
You can name solution folder with diffrent name then Your project. when You add more projects every project folder will be in one solution folder and path will looks like:
http://XXXXX/Solution/Project1
http://XXXXX/Solution/Project2
And one last thing: You probobly don't want whole solution in application server, you can just deploy project as web deploy packege.

Add as link intellisense for Views

I'm trying to share some views across projects by using the "Add as Link" feature in VS2015 but the linked view appears broken in VS even though it works fine on the site.
Is there a way to get features like intellisense and "go to definition" working for a linked View?
I can reproduce this with a new solution/project:
Move Home/Index.cshtml to some other folder (currently I'm using a Solution Folder but it doesn't seem to matter where I move the files)
delete Index.cshtml from Home/
from Home/ right click and "add existing item"
choose Index.cshtml from the Solution Folder but choose "Add as Link" from the button drop down
on the linked Index.cshtml choose properties "Copy to Output Directory = Copy Always"
The site will work fine but in VS2015 the view then appears broken and no intellisense works.
This would be a great way to share a view across multiple projects but if VS thinks the view is broken it makes developing a pain.
Any suggestions? Is there a better way to share views across projects?
Here's the painful way I got it to work:
Unload the solution
Create a copy of one of the projects containing the views I want to replicate. Let's call the new project [SharedItems].
Rename the .csproj file and edit the .csproj file manually and replace the old name with the new name (probably optional, but made things easier)
Load the solution and add the new project.
Delete any files you don't need.
Rename the namespaces in the new project.
Build just to make sure it's working and Intellisense is working correctly.
Delete the files you intend to link in the original projects and create links.
Installed the MSBuild.WebApplication.CopyContentLinkedFiles NuGet package on all the original projects to automatically make physical copies of the linked files.
Now I can do all my edits in the templated project without having to maintain multiple copies everywhere.
Two big downsides:
I have to build after making edits to any linked object before I can see the changes.
I have to open the actual .cshtml files in the [SharedItems] project only. Opening a linked file breaks Intellisense until you close it and open it directly. This is especially annoying with Resharper's Search Everywhere feature because it suggests linked files.

VS 2013 - pdb files are added automatically to source control

I have a solution with a web site project and two library projects (.dll).
The web site has references to the two dlls. The references are added as project references.
The web site also has a binary reference to log4net.dll.
When we build the solution the pdb files from the two project references is automatically added to TFS source control under the web site Bin folder.
How can I prevent this from happening?
Peter B. Frederiksen
By defaut a Web Site project has no option to include or exclude files. As such they do not work well with source control systems and are not recommended for use.
You need to flip over to a Web Application. This is simple but has ramification.
Create an empty web application
Update the settings and assemblyinfo to be what you want
Copy only the Project file and the AssemblyInfo files into the Web Site location (maintain folders.)
Add the new project to your solution with "open existing"
Make work / build
Remove web site from solution
Remember that your files are now pre-built and you may need to fix duplicate class names that are allowed in web sites.
It looks like from the diagram above that your bin directory was somehow checked in as part of source control, this directory should be deleted (bin) from tfs unless you intend to version the output, which in this case you should not.

How do you share scripts among multiple projects in one solution?

In case the question wasn't clear. I have 3 MVC projects in one Solution. Every time I create a new project it adds the "Scripts" folder with all the .js files I'll ever need. I don't want to have this created every time for every application. Is there a way to reference scripts from a central folder in the solution so all applications/projects can share one common script folder with all the scripts common among them?
Edit:
Please explain the pros and cons of doing this if there are any...now I'm curious.
Here is what I would recommend:
Right click the solution and create a New Solution Folder called Common Javascript Files (or whatever you feel like calling it.
Right click on the Solution, click Open Folder in Windows Explorer,
or navigate there manually for other versions of Visual Studio :(
In the solution directory, create a directory with the same name as the solution folder (solution folders do not normally match directories at the source code level but this will for sanity sake).
In this new directory, add files that need to be shared between solutions.
In Visual Studio, click the solution folder and select Add - Existing Item.
In the file selection dialog, navigate to the directory previous created, select the file(s) added to the directory and click Add.
In each Project that needs a shared file, right click on the project (or directory within the project) and click Add - Existing Item.
Navigate to the shared Directory, Select the files and click the drop down arrow then click Add As Link.
Now the files in the projects are essentially short cuts to the files in the Solution Folder. But they are treated as actual files in the project (this includes .CS or Visual Basic files, they will be compiled as files that actually exist in the project).
PROS
Files are truly shared across projects at Design time
Only the files needed for each project can be added, it's not all or nothing
Does not require any configuration in IIS (virtual directory etc)
If the solution is in TFS Source control, you can add the Directory to the TFS Source and the shared files will be source controlled.
Editing a file by selecting it in the Project, will edit the actual file.
Deleting a Linked file does not delete the file.
This is not limited to JS files, linked files can be ANY file you might need (Images, Css, Xml, CS, CSHTML, etc)
CONS
Each deployment gets it's own file.
There is a small learning curve when understanding that Solution Folders are not Directories that exist in a Solution Directory.
The best thing to do, imo, is to roll your own CDN... Basically just create another site in IIS and give it it's own binding, e.g. "http://cdn.somedomain.com"
Then store all of your css/js/fonts/shared images etc on the CDN site and link to them from your other sites.
Doing so solves 2 problems,
All of your stuff is shared when it needs to be and you only have to manage 1 revision per file.
Your users browsers can cache them in 1 single location instead of downloading copies of your stuff for every site that uses them..
I added this answer because I see a lot of people referrencing creating virtual directories. While that does indeed share the files, it creates multiple download paths for them which is an extreme waste of bandwidth. Why make your users download jquery.js (1 * number of sites) when you can allow them to download it once on (cdn.somedomain.com).
Also when I say waste of bandwidth, I'm not just talking about server bandwidth, I'm talking about mobile users on data plans... As an example, I hit our companies HR site (insuance etc) on my phone the other day and it consumed 25mb right out the gate, downloaded jquery and a bunch of stuff 5 times each... On a 2gb a month data plan, websites that do that really annoy me.
Here it goes, IMO the best and easiest solution, I spent a week trying to find best and easiest way which always had more cons than pros:
Resources(DLL)
Shared
images
image.png
css
shared.css
scripts
jquery.js
MvcApp1
Images
Content
Shared <- We want to get files from above dll here
...
MvcApp2
Images
Content
Shared <- We want to get files from above dll here
...
Add following to MvcApp1 -> Project -> MvcApp1 Properties -> Build events -> post build event:
start xcopy "$(SolutionDir)Resources\Shared\*" "$(SolutionDir)MvcApp1\Shared" /r /s /i /y
Here is explanation on what it does: Including Build action content files directory from referenced assembly at same level as bin directory
Do the same for MvcApp2. Now after every build fresh static files will be copied to your app and you can access files like "~/Shared/css/site.css"
If you want you can adjust the above command to copy scripts from .dll to scripts folder of every app, that way you could move some scripts to .dll without having to change any paths,here is example:
If you want to copy only scripts from Resources/Shared/scripts into MvcApp1/scripts after each build:
start xcopy "$(SolutionDir)Resources\Shared\Scripts\*" "$(SolutionDir)MvcApp1\Scripts" /r /s /i /y
This is a late answer but Microsoft has added a project type called Shared Project starting Visual Studio 2013 Update 2 that can do exactly what you wan't without having to link files.
The shared project reference shows up under the References node in the
Solution Explorer, but the code and assets in the shared project are
treated as if they were files linked into the main project.
"In previous versions of Visual Studio, you could share source code between projects by Add -> Existing Item and then choosing to Link. But this was kind of clunky and each separate source file had to be selected individually. With the move to supporting multiple disparate platforms (iOS, Android, etc), they decided to make it easier to share source between projects by adding the concept of Shared Projects."
https://blogs.msdn.microsoft.com/somasegar/2014/04/02/visual-studio-2013-update-2-rc-windows-phone-8-1-tools-shared-projects-and-universal-windows-apps/
Info from this thread:
What is the difference between a Shared Project and a Class Library in Visual Studio 2015?
https://stackoverflow.com/a/30638495/3850405
A suggestion that will allow you to debug your scripts without re-compiling the project:
Pick one "master" project (which you will use for debugging) and add the physical files to it
Use "Add As Link" feature as described in Eric's answer to add the script files to the other projects in solution
Use CopyLinkedContentFiles task on Build, as suggested in Mac's comment to copy the files over to the second over to your additional projects
This way you can modify the scripts in the "master" project without restarting the debugger, which to me makes the world of difference.
In IIS create a virtual folder pointing to the same scripts folder for each of the 3 applications. Then you'll only need to keep them in a single application. There are other alternatives, but it really depends on how your applications are structured.
Edit
A scarier idea is to use Areas. In a common area have a scripts directory with the scripts set to be compiled. Then serve them up yourself by getting them out of the dll. This might be a good idea if you foresee the common Area having more functionality later.
Most of the files that are included by default are also available via various CDN's.
If you're not adding your own custom scripts, you may not even need a scripts directory.
Microsoft's CDN for scripts: http://www.asp.net/ajaxlibrary/cdn.ashx

.Net Build does not contain output folders from other projects

We had a Project in our solution called DatabaseCreation - which was responsible for creating the database and running the scripts (contained in directories within the project).
Everything was fine.
We then wanted to make the creation of this database to be available from an app and from a console, so re-factored so that we had ...
DatabaseCreation.Core
DatabaseCreation.App
DatabaseCreation.Console
Core has the directories containing scripts, and the App and Console run them.
When built with Visual Studio the directories containing the scripts are copied to the Output bin directory (either App or Console) but problem is: when the build server runs, the script directories don't get copied to the bin folder.
We've tried with TFS and Team City. I realise I could just copy them over, but its frustrating that it works when building in Visual Studio!
Can anyone put me on the right track please?
Check the Build Type for the files in the scripts directory. Find that by clicking one or many files (ctrl+click) then hitting properties (often f4). You likely want them to be content with Copy Always or Copy if newer.
The files were set to copy always, but the build just wouldn't pick them up.
The slightly awkward work around was to add the files as links from one project to another.
Add Existing -> navigate to other project - Looking for the little arrow on the right of the add button -> press that, Add as link option appears.

Resources