Friday, 30 October 2015

Razor views and publishing woes



If you've done any ASP.NET MVC work, especially in VS2015, you may have encountered this:

  •  You’re devving away on a controller and return View(withSomeNiftyModel).
  • You alt-enter or alt-. to get VS to create the view
  • You make some amazing html view. It has tags and content and everything!
  • You test the view using an F5 or Ctrl-F5 and it’s doing what you want and looking how you want (though your designer, who may or may not be named Jess, probably has sterner words about how it looks)
  • Some time later, you build a deployment package and upload to the client.
  • If the internet gnomes are with you, soon you will deploy at the client. If not, you’ll wait… and then deploy at the client
  • And then you get the dreaded “view not found error page”:







Well, you don’t have to continue with this sad existence. You could add this snippet to your web project’s .csproj file:

  <Target Name="EnsureContentOnViews" BeforeTargets="BeforeBuild">
    <ItemGroup>
      <Filtered Include="@(None)" Condition="'%(Extension)' == '.cshtml'" />
    </ItemGroup>
    <Error Condition="'@(Filtered)'!=''" Code="CSHTML" File="$(MSBuildProjectDirectory)\%(Filtered.Identity)" Text="View is not set to [BuildAction:Content]" />
  </Target>

(Place it after the ItemGroup which contains your views, if you’re looking for a spot to put it)

And then, voila! Your project doesn’t build unless all views are set to build action content:



Kudos to the solution here: http://stackoverflow.com/questions/27954267/make-sure-all-cshtml-files-are-set-to-be-content-for-build-action (though I’ve upgraded from a warning to an error, because, well, it should be).


No comments:

Post a Comment

What's new in PeanutButter

I realise that it's been a while (again) since I've posted an update about new things in PeanutButter ( GitHub , Nuget ). I've ...