Posts in  mvc

1.30.2014

Disable encoding with custom html builders in Razor without using IHtmlString

If you use a custom html builder, not marked with IHtmlString, and don't want to use the verbose @Html.Raw(builder) and would rather just use @builder, see this gist for how to set this up:


7.23.2013

Adding OWIN to an empty ASP.Net MVC app

I've been playing around with the new ASP.Net MVC 5 samples and wanted to share how you can quickly inject OWIN into an empty MVC app and for that matter any ASP.Net app. I'll admit I haven't found a ton of documentation about this yet so this is just me hacking around and wanting to share what I found.

  1. Start out by making an empty MVC project.
  2. Install Microsoft.Owin.Host.SystemWeb
    • Install-Package Microsoft.Owin.Host.SystemWeb -Prerelease
    • This has a type OwinHttpModule that gets loaded dynamically via Microsoft.Web.Infrastructure that "injects" the OWIN infrastructure.
  3. Add a Startup class to configure OWIN
    • This is where you configure your OWIN middleware, this is by convention
          public class Startup
          {        
              public void Configuration(IAppBuilder app)
              {
              }
          }
  4. Do something with OWIN
    • Install-Package Owin.Extensions
      • Nice extension methods to simplify the verbosity of creating middleware
    • I found these ideas in Getting Started With OWIN, Katana, and VS2013
    • Something simple like this can interrupt the process and return a result from OWIN middleware app.UseHandler((request, response) => response.WriteAsync("OWIN Hello World"));
      • This is very similar to the style you'll find in nodejs express apps.
    • This will allow the request to pass through the OWIN infrastructure and be handled by MVC but it will tack on a header to the result.
      app.UseHandler((request, response, next) =>
      {
          response.AddHeader("Hello", "World");
          return next();
      });