ASP.NET MVC 5 Tips and Tricks

Here're a bunch of random tips that I've found useful while working with ASP.NET MVC 5:

  • Use the OutputCache action filter to cache the output.
  • Use the HandleError action filter for error pages.
  • Use @Styles.Render(...) and @Scripts.Render(...) to render <link> and <script> tags.
  • Use @RenderSection(“name”, required: false) instead of @if(IsSectionDefined(“name”)) { ... }.
  • Use @Html.AntiForgeryToken and ValidateAntiForgeryToken action filter to avoid CSRF attacks.
  • @Html.Partial()'s second argument can contain "template" data.
  • Use BundleTable.EnableOptimization = true for cache busting and minification. See App_Start/BundleConfig.cs.
  • ScriptBundle and StyleBundle can also .Include CDN paths.
  • _ViewStart.cshtml is used to specify common properties across all views.

  • mvc
  • razor
comments powered by Disqus