Add as link intellisense for Views - asp.net-mvc

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.

Related

MVC Shared layout between projects

I was wondering if there is a way to share the _layout.cshtml file across all projects. This way if I ever wanted to make a change to our companies page layout I only have to change it in one place. From the research I've seen so far people have made mention of possibly creating a .dll file. Also I'm not talking about areas in mvc. What I want is for all my separate applications to reference one shared layout page instead of each application having it's own layout.
Once pattern (which I've been involved in the implementation of) is to make a Nuget package containing the layout file (and anything else you want to share in common between all your sites, such as other CSS files, error handling routines, etc), and installing the package (from a private feed) into each project which requires it, thus overwriting the default layout file).
The source of the Nuget package is itself a C# MVC project with a custom nuget config which makes sure that it includes just the necessary content from the C# project into the distributable nuget file.
Apart from ease of installation and separation of concerns, another big advantage of distributing via Nuget is that if you make changes to the source files and update the package, each application which has the package installed can choose when would be a good time to update to the latest version (if at all), so you don't have issues where if you change one thing you have to change everything which depends on it all at the same time (For instance, the Add as Link option in Slappywag's answer would suffer from that problem - if you changed the layout file in that scenario it would immediately propagate to all the projects which use it, so if you wanted to make any other changes in that project you'd have to deal with any implications of the layout change before you could publish your other changes, which might not be desirable/convenient at the time.)
The way I've achieved this in the past is to use 'add as link'.
Firstly, add your layout files to a new library which can be shared between your different projects. Then in each of your projects which you want to use the layout page add the layout page as a link:
In solution explorer right click on the folder in whcih you want to place your layout file.
Click add existing, and select the layout file you want to use.
Click the little arrow on the right hand side of the 'Add' button, and select 'Add as link' from the dropdown. Your layout file will now be in place.
Depending on how your project is set up you may need to follow the steps in this answer to ensure your linked files are copied at build time. You may also want to make sure the copied files are being ignored by your source control.
Update your ViewStart to use the new layout file.
Any changes you need to make to the layout can be made to the original file, and on build changes will be picked up by your projects.

Certain Razor views not publishing

Using VS 2017 with MVC 5 Razor views. When I publish my application, a handful of specific views do not get copied over.
I'd discovered several SE questions on this same issue from back in the 2010-2011 timeframe. At the time, the issue was that Build Action in the file's properties was not set to Content due to a bug in some early RC which has since been resolved. Well, all of mine already do day Content for Build Action.
Any reason why only a small number of views are not making it in the publish?
As far as I'm aware, there are only 2 things that can cause this to happen.
As you say in the question, the build action for each view needs to be set to "Content"
The view files need to be included in the project file, so in the .csproj file there should be a line like this:
<Content Include="Views\ControllerName\Index.cshtml" />
Another one would be to set this in your csproje file. This was missing in mine, which caused it to make a precompiledviews.dll in my publish:
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
Set it to false explicitely in order to publish .cshtml files.
Visual Studio -> Right click on "Views" in solution explorer. Click "Publish Views". This will set all the views to Content"
Depending on the version of Visual Studio, excluding and then including partial view or its folder may work or not. The most certain way to do it is to remove it from .csproj file and then include it again.
lets say _LoginPartial is not updated after publishing (especially if you have some razor code).
First open '.csproj' file, and remove
<Content Include="Views\Shared\_LoginPartial.cshtml" />
The build the project, and then add it again to .csproj, and rebuild and publish.
Another Solution From Visual Studio
right-click on the file and "Exclude From Project".
select the Project and click on "Show All Files" from Solution Explore (top horizontal menu bar on Solution Explore)
right-click on the file that Excluded and Click on "Include In Project".
This will automatically modify the .csproj file.

How to organize CSS,JS and other files into ASP MVC 4

I am totally new to ASP.NET MVC 4 as I was developing into PHP (Laravel) and the way to organize things is usually something like a "public" folder which would then be separated into css, img, js, lib, and so on.... which is the way I prefer to organize my files...Trying to follow this into ASP MVC 4, and I found and read this question that is similar to what I want achieve. organizing custom javascripts in asp.net mvc 4I did it the way they said and added my custom JS files and added them in the Bundle, it compile and also run fine but the problem I have is that inside the Solution Explorer all the files that I moved are showing with an exclamation mark and also my newly created folders (directly in file explorer, not doing it through VS) are not showing by default, they are for some reason hidden, which I can see with Show All Files. I made sure that they exist inside the Bundle but then my Solution Explorer does not get refreshed. I am trying to make a structure that looks the following:
Contents
img
ui-icons.png
css
bootstrap
bootstrap.css
bootstrap.min.css
Scripts
lib
bootstrap
bootstrap.min.js
bootstrap.js
jquery
jquery.js
jquery.min.js
jquery.ui.js
jquery.ui.min.js
modernizr
modernizr.js
custom
mycustom.js
mycustom2.js
I am not crazy about dumping everything inside the same folders (Content & Scripts), including my custom files in between some official libraries. I know using directly the File Explorer is probably not the best way to go, but then what would be the official way of configuring these... and actually does my structure make sense into ASP MVC4?
EDITMy question is more related to why after creating and moving the files like jquery and others into a Scripts/lib folder, my Solution Explorer is still showin these files under Scripts but with an exclamation marks as saying file not found (of course since I moved them). Why it doesn't reflect exactly what I see in File Explorer? Apart from editing the Bundles is there anything else I'm suppose to do so that my Solution Explorer is up to date with reality??? and why are my created folders not showing in Solution Explorer, why do I have to click on Show All Files to see them? The commmand Create New Folder is not even enable as an available command, that's why I have done these folders directly in File Explorer...but why?
Frankly, you can organize your MVC project however you see fit. If there's anything close to an "official" way to organize your web files, it would be how the default Visual Studio MVC4 template organizes them:
Content (CSS files and related image files)
Images (general image files)
Scripts (JavaScript files)
If you're more familiar with another sort of organization or are part of a team that would prefer another organization, then go for it. (Though in that team scenario, make sure all the team members follow the same organization rules!).
Edit:
To alter the folder structure that the MVC template provides using the Solution Explorer, right-click on the folder in which you want to add a subfolder (this includes the project name, for project-level folders), and then select Add and then Add Folder from the pop-up menu that appears.
If you want to move around files that are already in your solution to another location within the solution, you should move them around using the Solution Explorer, not Windows Explorer, as you'll otherwise get the behavior you are experiencing, where Visual Studio does not know where you moved them. You can click and drag files around, right-click and copy and paste, etc.
If you need to add pre-existing files to your solution (say, to include a set of custom scripts), you can copy the files to the appropriate project folder using Windows Explorer. Then, in Visual Studio, highlight the project that corresponds to where you moved them, and press the Show All Files button in the Solution Explorer toolbar - this will show the files you copied in Windows Explorer, which otherwise won't be listed in Solution Explorer tab. Lastly, highlight the new files, right-click, and select the Include in Project option from the pop-up menu.
One final pointer: if you need to add a specific JavaScript library to your project, the easiest way to do so would be to use the NuGet package manager, rather than to download and add the files in Windows Explorer. This option is found in Visual Studio in the Tools menu, under Library Project Manager --> Manage NuGet Packages for Solution. Not all JavaScript libraries will be available this way, but the most popular ones are.

TFS Linked Files Not Found

I have a core project and several sub projects. I link to files in the core project for the sub ones. I righted clicked my csproj and said existing file > add > link. Here is how my project looks:
Here is a picture of my Windows Explorer:
When I check in, I receive this error:
Could not find file C:\Users\joe_a84\Documents\JMASoftware\QuickBooks\Main\Source\Platform Support\NOP\Source\Versions\nop265\Nop265\Module\QuickBooksSettings.cs
The file does not exist there because it is linked. How can I tell TFS that it's a linked file?
There is the following question which states that symbolic links are not supported in TFS 2008 or TFS 2010. Symbolic links in TFS 2010 Source Control?
However, I did find the following ancient blog post which seems to show how to do it for TFS 2005: http://blogs.msdn.com/b/ericlee/archive/2006/07/20/sharing-files-in-team-foundation-server.aspx
That said I would highly recommend that you simply don't do this.
If you really need to share a file between projects then you should be using the branching functionality built into TFS. Typically you don't want just a single code file due to namespace and assembly generation concerns. It is much cleaner, and certainly easier to maintain, by having a separate project for those artifacts that are passed around. This way you either branch the entire assembly project; or just refer to it in it's normal place.
This is a local file level linking by another project. There is only one file at a specific subfolder. Other projects can link to the file via this approach. The problem with this approach is that from TFS viewpoint, you cannot tell if a file is shared by other projects. A good practice will be to keep such files in separate folder with name like Common or Shared etc.
People who have been using File linking across branches in Source Control would have to change their approach quite differently.
Linking between files in TFS is pretty simple. You just open the "Add existing item" dialog and search for the file you want to link in your local workspace. If you would click "Add" now Visual Studio would create a copy of the file in your projects directory. What you do instead is to choose the other option (you have a little drop down menu on the button) and add the item as a link. Thats it.
I faced the same problem. I just did undo on the file which was showing this error in the TFS Pending Changes window and checked-in. This undo did not remove the link that was added in the project. Make sure you do not undo the project file.

Missing MVC templates in existing web project

In an existing VS2008 SP1 web project I'm introducing an MVC page. I was able to manually create a view and get it working just fine but I'm unable to get the "Add View" and "Add Controller" to work from the Solutions Explorer. Also, the MVC templates don't show up when I try and Add a New Item.
I know my VS2008 is setup properly because on a new project everything works fine (MVC templates show up in Web Items) and the "Add View" and "Add Controller" work fine as well. Is there a trick to get my legacy project to see these options.
I tried look around the csproj files. Nothing obvious there.
I see the template zip files in my folder ...Common7\IDE\ItemTemplates\CSharp\Web\MVC
Thanks.
My guess is that the project type is probably set wrong. I know it did change after one of the betas.
Try this: First, check in your existing files to source control, in case you accidentally mess up the project file. Open both of the *.csproj files (the one that "works" and the one that "doesn't") as text. You will have to unload the projects in the IDE in order to do this. Copy the <ProjectTypeGuids> value from the one that "works" to the one that "doesn't". Now reload the projects. See if that fixes the problem.

Resources