This project is read-only.

Set the proper properties for RazorGenerator by default for view files

Jan 7, 2012 at 11:33 PM


I am trying out this awesome project to see if I can use this for my corporate projects and I will do that definitely.

One thing to ask: when I add a new view on my ASP.NET MVC, it does not set the proper properties for RazorGenerator. I need to do that manually. Is there any way that I can set the default to be RazorGenerator for Custom Tool and None for Build Action

Jan 7, 2012 at 11:40 PM

I'm not sure if that's possible (though it may be with proper DTE automation). However, you can run the 'Enable-RazorGenerator' command from the nuget Package Manager Console, which is more convenient than doing it manually (see doc).

Jan 7, 2012 at 11:51 PM
Edited Jan 7, 2012 at 11:52 PM

Enable-RazorGenerator is awesome. I didn't know it was there:) That is just what I needed. Thanks!

BTW, just for the fun, would it be possible to write a recipe in order to do that operation (although, there is no need when there is a PS command that we can use)?

Jan 7, 2012 at 11:59 PM

Also, I noticed something. It sets CustomTool property to RazorGenerator but not setting the Build Action to None.

Is it possible to do that with the powershell code just like you did as below:

function Set-CustomTool {
    param($item, [string]$customTool, [bool]$force = $false)
    $customToolProperty = $_.Properties.Item("CustomTool")
    if ($force -or !$customToolProperty.Value) {
        $customToolProperty.Value = $customTool
        return $true

Jan 8, 2012 at 12:31 AM

Played around a little but failed:s Is there any Visual Studio Power Shell property reference out there?

Jan 8, 2012 at 1:00 AM

ok, hang in there. I found a way I guess.

Jan 8, 2012 at 1:10 AM
Edited Jan 8, 2012 at 1:20 AM

Finally, get it worked.

Here is the private function(I followed your lead):


function Set-BuildAction {
    param($item, [bool]$force = $false)
    $buildActionProperty = $_.Properties.Item("BuildAction")
    if ($force) {
        $buildActionProperty.Value = 0
        return $true


and this is the Enable-RazorGenerator function:


function Enable-RazorGenerator {
        [parameter(ValueFromPipelineByPropertyName = $true)]
    Process {
        $solutionExplorer = ($dte.Windows | Where { $_.Type -eq "vsWindowTypeSolutionExplorer" }).Object
        $project = (Resolve-ProjectName $projectName)
        $projectPath = [IO.Path]::GetDirectoryName($project.FullName)
        $ProjectName = $project.Name
		$SolutionName = [IO.Path]::GetFileNameWithoutExtension($dte.Solution.FullName)
        Get-RazorFiles $ProjectName | % { 
            if ((Set-CustomTool $_ "RazorGenerator") -and (Set-BuildAction $_)) {
                $relativePath = Get-RelativePath $projectPath $_
                if ($relativePath) {
                    $solutionExplorer.GetItem("$SolutionName\$ProjectName$relativePath").UIHierarchyItems.Expanded = $false

It worked nicely but I am not sure it is usable code on prod.

Jan 8, 2012 at 5:56 PM

Thanks for looking into this!

Couple comments:

  • Seems you never return true from Set-BuildAction unless force is passed (and it's not passed). Or am I misreading this? Note that collapsing the UIHierarchyItems is probably only needed when setting the custom tool, and not when setting the BuildAction. So maybe they should be in the same 'if' statement.
  • We support some fallback scenarios where you do want the build action to be Content (see PrecompiledMvcEngine.UsePhysicalViewsIfNewer). So maybe that should be a switch on Enable-RazorGenerator.


Pranav knows this code better and can probably comment further.

Jan 8, 2012 at 11:43 PM
Edited Jan 8, 2012 at 11:46 PM

Thanks for the response. After I submitted the above code, I had a second thought on those issues either. So I tidied up a little and foked the repo. 

Earlier today, I managed to send a pull request. Here is the code I ended up with:

Fork: RazorGenerator, Change Set ecc14b5864d0

I thought that would be too aggressive to change the behavior of the Enable-RazorGenerator function so I added a switch which can be supplied. So the below function sets the file BuildAction property to None as well as the default functionality:

Enable-RazorGenerator -isBuildActionNone $true

I am not sure if the code has a place on prod but that would be nice to have that functionality. 

I would be happy if you can update the package with a functionality something like this.

Thanks for this awesome project!

Jan 9, 2012 at 1:48 AM

See my comments in the pull request.