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).


Thursday 29 October 2015

Please welcome PeanutButter 1.0.118!

 (if you don't know what PeanutButter is -- apart from a yummy spread and a rather silly name for an open-source project -- have a gander here: http://davydm.blogspot.co.za/2014/06/introducing.html)

What's new:
  • Updates to DatabaseHelpers:
    • Fluent syntax for conditions (.And() and .Or() extension methods)
    • EqualityOperators changes
      • Like_ is deprecated (but still functions). It is replaced by Contains, with the same functionality
      • Like is new: does not insert any wildcards -- that's up to you! (also, finally, has a reasonable name, now that I know how to use keywords as identifiers in VB.NET. Yes, the DBHelpers are written in VB.NET. Get over it.)
      • StartsWith: does what it says on the tin
      • EndsWith: also does what it says on the tin
    • Added ConcatenatedField for when you want some composite result from a few fields
    • Updated CalculatedField: All *Fields now implement IField, so all of the existing funcationality which SelectFields could give you can also be found for the others (conditions, etc)
  • Enumerable convenience functions
    • And() -- like Union() but produces an Array
      • eg new[] { 1, 2 }.And(3) is equivalent to new[] { 1,2,3}
    • ButNot() -- like Except(), but produces an Array
  • RandomGenerators:
    • RandomValueGen.GetRandomCollection
      • Takes a factory function and optionally min/max number of items
      • eg to get a random-sized IEnumerable<int> of random ints, do:
        RandomValueGen.GetRandomCollection(() => RandomValueGen.GetRandomInt())
    • RandomValueGen.GetRandomSelectionFrom
      • picks some random items from another collection
  • ExpressionHelper: 
    • GetMemberPathFor:
      use to get full property paths from lambdas, eg
      ExpressionHelper.GetMemberPathFor<Foo>(fooItem => fooItem.SomeProperty.SomeValue)
      gets you
      "SomeProperty.SomeValue"
      Useful for testing or somewhere where you need to get to properties dynamically
    • GetPropertyTypeFor:
      Similar usage to GetMemberPathFor, but returns the property type
  • Of course, there are some bugfixes
  • And probably other stuff I forgot... It's been a while since I tooted on the PeanutButter horn.
 Like other PeanutButter bits and pieces, these have been born out of a desire for convenience, cleaner code and/or getting the compiler to check stuff before hitting the runtime. I hope they benefit others -- I know that I use a little PB every day (:

As always, usage, source availability and distribution remain totally free.

What's new in PeanutButter?

Retrieving the post... Please hold. If the post doesn't load properly, you can check it out here: https://github.com/fluffynuts/blog/...