This question has probably been asked before but my search failed to turn anything up. Is there an easy way to find out whether a view/partial is not being used in an MVC project? Currently I'm searching for the individual view name across the entire solution but wondering whether there's a tool out there that would make this job easier? I'm ideally looking for something that would provide a visual indication within Visual Studio if a view/partial is not being used.
I'm basically cleaning up an existing codebase and want to rip out any views that are not being used any more.
When I want to see if a view is used, I move it out of my project and enable view compilation. If a view is referenced from somewhere else it usually breaks.
enable view compilation by manually editing your mvc csproj file and setting the following
<PropertyGroup>
<MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
Related
I'm trying to write a module/plugin system for a website I'm working on. The overall goals are:
That the main website does not have to be recompiled every time a new plugin is added.
To be able to dump DLL's and CSHTML files into a set of folders that would basically add a set of routes, controller(s), and any other assemblies that the module depends on (Entity Framework, etc).
To avoid marking plugin files as "embedded resources" -- especially views.
To be able to add and remove these modules/plugins WITHOUT having to restart/recycle the IIS app pool or unload the app domain.
I got #1-3 working by following the example of Umbraco. Basically, I marked a method with the PreApplicationStartMethod attribute, and in it I shadow copy DLLs around, and use a custom ViewEngine to locate the module's CSHTML files. When my website first starts up, my module's controllers and views are working, and assemblies are loaded: Hooray!
However, when it came time to try part #4, I am getting this error when calling BuildManager.AddReferencedAssembly():
This method can only be called during the application's pre-start
initialization phase. Use PreApplicationStartMethodAttribute to
declare a method that will be invoked in that phase
It's been a very frustrating process so far, and my gut tells me that this error signifies a dead end. Is this the case, or is there a clever workaround?
Editing the web.config file when you add the new modules should cause the site to recompile.
You could automate this in a script, forcing your new .dll's to be copied to the live ASP.NET temp files directory.
Check out portable areas. Essentially a regular MVC area (including views, controllers, etc.) gets compiled into a single dll. These dll's (one for each area) can be dropped into a hosting MVC website and can be called like any other MVC area.
Some references to get started:
Portable Areas three years later – Part 5
MvcContrib Portable Areas
"To be able to add and remove these modules/plugins WITHOUT having to restart/recycle the IIS app pool or unload the app domain."
It turns out that this you cannot unload an assembly from an app domain.
How to unload an assembly from the primary AppDomain?
I was wondering if Razor views could be compiled, like WebForm based views?
Does it even make sense to compile Razor views and why would somebody want to do that?
Edit:
Here is a blog post on this topic as well:
How to Detect Errors of Our ASP.NET MVC Views on Compile Time
To make your views to be compiled, do the following;
Unload your project by right
clicking the project on the solution
explorer in VS and clicking unload
project
right click the project which has
been converted to unavailable
project and click "Edit
your_project_name.csproj" (that
would be .vbproj if your
project is VB project)
see the following code;
<!--There some lines of code here and I deleted them to get to the point quickly-->
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
change the MvcBuildViews tag value from false to true
after that save it and reload your
project.
after you build your solution to compile it, you will see that your view will be compiled too.
NOTE: to test it, break some code in one of your view on purpose and try to build. you will see that you'll get an error message.
The MvcBuildViews check is excellent but it adds a 5-10 second penalty for building your web app if it's complex enough. It doesn't cache the compilation output so it does a full compilation of all your views every time.
I found a nice compromise by following the above advice and adding a Condition attribute:
<MvcBuildViews Condition=" '$(Configuration)' == 'Release' ">true</MvcBuildViews>
We'd expect ReSharper to flag up any errors in the views anyway and the developer can always build in the release configuration as a test - we have a "preflight" script that developers run so they can easily make sure that package targets work and so on - and if all that fails, the build server will catch it.
Perhaps this trick is obvious but I've only really started learning about msbuild properly as opposed to writing Powershell scripts for these tasks. I hope this is helpful to someone.
Yes, you can. Take a look at the following post: Compile your asp.net mvc Razor views into a seperate dll
It's a "step-by-step" guide on how to compile your razor views into a separate dll. I don't know if that's what you aim to do but it'll definitely get you in the right direction.
Yes, it's possible. In fact, the best example I can think of would be email templating engines. If you compile and cache the template, then you can quickly rip off emails without having to go through the parsing all over again.
That's a good example of using Razor outside of MVC as well.
I am finalizing a MVC project and have switched to using Google's CDN for jQuery. After doing that, do you delete out the unused .JS files from the scripts folder?
Did you leave all the extra default plumbing visual studios adds for the controllers, views, models that no one ever uses? (i sure hope you answered no)
that should answer this question to. no sense having stuff your not using laying around.
I personally keep them around, so that if I go back to development and want to reference the scripts locally again, I can do that without getting them in place all over again.
I'd leave them around whilst developing (just in case...) but once finished and sure I won't need them I'd delete them. It's only going to cause confusion for other developers if they assume you are referencing them locally as they've seen them in the Scripts folder. If you are really squeamish about deleting them, I guess you could always exclude them from the project - but really I see no point so delete 'em!
I want to take a look at my views with reflextor, I have set my project to compile views
<MvcBuildViews>true</MvcBuildViews>
But when opening the DLL for my application in reflextor I do not see the views...
I see the controllers and models but no views...
Have I not compiled the views correctly? or is there more to it?
Thanks,
If you want a fully compiled ASP.NET MVC project with your views compiled, you can either run aspnet_compiler.exe against your web app.
That's the hard way. The easy way is to install the Web Deployment Project add-in to Visual Studio. Then you can add a web deployment project and set it to fully compile your web application.
http://www.microsoft.com/downloads/details.aspx?FamilyId=0AA30AE8-C73B-4BDD-BB1B-FE697256C459&displaylang=en
It's really easy to use and gets you full compilation.
Is there any chance the views are just being compiled in order to provide you with compile time checking but not actually being included in the assembled output?
It seems like the main goal of this feature could be just to help catch compile time errors quicker not necessarily to speed up the processing of the page or anything.
Views are not compiled in DLL, they are copied as is to the output. Attribute that you use is only compile time check for them.
In WinForm and WebForm Application, Visual studio groups the 'View' File and 'Contorller' File, together. I found very easy to manage file in that way.
Now in ASP.Net Mvc , i'm finding difficult to manage views, separately in completely different folder.
Is there any way to group View and Controller files in VS Solution Explorer, like we used to do in WinForm and WebForm Application ?
I think they did that on purpose - views should be independent from controllers. Think of it like this: you should be able to put controllers into a totally different assembly and still have your application work. Your controllers should also be able to work with totally different set of views.
The framework is also setup to go to the views folder to fetch appropriate files. You would have to change that behavior yourself if you decide to move the views. Might not be worth the hassle.
And finally, if you really want to do it, you should probably look at your project file. There is a DependsUpon element that you can use to make a file go underneath another:
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
you can use VSCommands to group/ungroup files directly from Visual Studio