<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="https://shazwazza.com/rss/xslt"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Shazwazza</title>
    <link>https://shazwazza.com/</link>
    <description>My blog which is pretty much just all about coding</description>
    <generator>Articulate, blogging built on Umbraco</generator>
    <image>
      <url>/media/0libq25y/frog.png?rmode=max&amp;v=1da0e911f4e6890</url>
      <title>Shazwazza</title>
      <link>https://shazwazza.com/</link>
    </image>
    <item>
      <guid isPermaLink="false">1309</guid>
      <link>https://shazwazza.com/post/controller-scoped-model-binding-in-aspnet-core/</link>
      <category>ASP.Net</category>
      <title>Controller Scoped Model Binding in ASP.NET Core</title>
      <description>&lt;p&gt;Want to avoid &lt;em&gt;[FromBody]&lt;/em&gt; attributes everywhere? Don’t want to use &lt;em&gt;[ApiController]&lt;/em&gt; strict conventions? Don’t want to apply &lt;em&gt;IInputFormatter&lt;/em&gt;’s globally?&lt;/p&gt;
&lt;p&gt;ASP.NET Core MVC is super flexible but it very much caters towards configuring everything at a global level. Perhaps you are building a framework or library or a CMS in .NET Core? In which case you generally want to be as unobtrusive as possible so mucking around with global MVC configuration isn’t really acceptable. The traditional way of dealing with this is by applying configuration directly to controllers which generally means using controller base classes and attributes. This isn’t super pretty but it works in almost all cases from applying authorization/resource/action/exception/result filters to api conventions. However this doesn’t work for model binding.&lt;/p&gt;
&lt;h2&gt;Model binding vs formatters&lt;/h2&gt;
&lt;p&gt;Model binding comes in 2 flavors: formatters for deserializing the request body (like JSON) into models  and value providers for getting data from other places like form body, query string, headers, etc… Both of these things internally in MVC use model binders though typically the language used for binding the request body are called formatters. The problem with formatters (which are of type &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.formatters.iinputformatter?view=aspnetcore-3.1" target="_blank"&gt;IInputFormatter&lt;/a&gt;) is that they are only applied at the global level as part of &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.mvcoptions.inputformatters?view=aspnetcore-3.1#Microsoft_AspNetCore_Mvc_MvcOptions_InputFormatters" target="_blank"&gt;MvcOptions&lt;/a&gt; which are in turn passed along to a special model binder called &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.modelbinding.binders.bodymodelbinder?view=aspnetcore-3.1" target="_blank"&gt;BodyModelBinder&lt;/a&gt;. Working with &lt;em&gt;IInputFormatter&lt;/em&gt; at the controller level is almost impossible.&lt;/p&gt;
&lt;p&gt;There seems to be a couple options that look like you might be able to apply a custom &lt;em&gt;IInputFormatter&lt;/em&gt; to a specific controller:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a custom &lt;em&gt;IModelBinderProvider&lt;/em&gt; – this unfortunately will not work because the &lt;em&gt;ModelBinderProviderContext&lt;/em&gt; doesn’t provide the &lt;em&gt;ControllerActionDescriptor&lt;/em&gt; executing so you cannot apply this provider to certain controllers/actions (&lt;a rel="noopener" href="https://github.com/dotnet/aspnetcore/issues/21724" target="_blank"&gt;though this should be possible&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Assign a custom &lt;em&gt;IModelBinderFactory&lt;/em&gt; to the controller explicitly by assigning &lt;em&gt;ControllerBase.ModelBinderFactory &lt;/em&gt;in the controllers constructor – this unfortunately doesn’t work because the &lt;em&gt;ControllerBase.ModelBinderFactory &lt;/em&gt;&lt;a rel="noopener" href="https://github.com/dotnet/aspnetcore/issues/21724" target="_blank"&gt;isn’t used for body model binding&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;So how does [ApiController] attribute work?&lt;/h2&gt;
&lt;p&gt;The &lt;em&gt;[ApiController]&lt;/em&gt; attribute does quite a lot of things and &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#apicontroller-attribute" target="_blank"&gt;configures your controller in a very opinionated way&lt;/a&gt;. It almost does what I want and it somehow magically does this&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;[FromBody] is inferred for complex type parameters&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;That’s great! It’s what I want to do but I don’t want to use the &lt;em&gt;[ApiController]&lt;/em&gt; attribute since it applies too many conventions and the only way to toggle these …. is again at the global level :/ This also still doesn’t solve the problem of applying a specific &lt;em&gt;IInputFormatter&lt;/em&gt; to be used for the model binding but it’s a step in the right direction.&lt;/p&gt;
&lt;p&gt;The way that the &lt;em&gt;[ApiController]&lt;/em&gt; attribute works is by using MVC’s &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/application-model?view=aspnetcore-3.1" target="_blank"&gt;“application model”&lt;/a&gt; which is done by implementing &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/application-model?view=aspnetcore-3.1#iapplicationmodelprovider" target="_blank"&gt;IApplicationModelProvider&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;A custom IApplicationModelProvider&lt;/h2&gt;
&lt;p&gt;Taking some inspiration from the way [ApiController] attribute works we can have a look at the source of the application model that makes this happen: &lt;a rel="noopener" href="https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Core/src/ApplicationModels/ApiBehaviorApplicationModelProvider.cs" target="_blank"&gt;ApiBehaviorApplicationModelProvider&lt;/a&gt;. This basically assigns a bunch of &lt;a rel="noopener" href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.applicationmodels.iactionmodelconvention?view=aspnetcore-3.1" target="_blank"&gt;IActionModelConvention&lt;/a&gt;’s: &lt;em&gt;ApiVisibilityConvention&lt;/em&gt;, &lt;em&gt;ClientErrorResultFilterConvention&lt;/em&gt;, &lt;em&gt;InvalidModelStateFilterConvention&lt;/em&gt;, &lt;em&gt;ConsumesConstraintForFormFileParameterConvention&lt;/em&gt;, &lt;em&gt;ApiConventionApplicationModelConvention&lt;/em&gt;, and &lt;em&gt;InferParameterBindingInfoConvention&lt;/em&gt;. The last one &lt;strong&gt;InferParameterBindingInfoConvention &lt;/strong&gt;is the important one that magically makes complex type parameters bind from the request body like JSON like good old WebApi used to do.&lt;/p&gt;
&lt;p&gt;So we can make our own application model to target our own controllers and use a custom &lt;em&gt;IActionModelConvention&lt;/em&gt; to apply a custom body model binder:&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;
public class MyApplicationModelProvider : IApplicationModelProvider
{
    public MyApplicationModelProvider(IModelMetadataProvider modelMetadataProvider)
    {
        ActionModelConventions = new List&amp;lt;IActionModelConvention&amp;gt;()
        {
            // Ensure complex models are bound from request body
            new InferParameterBindingInfoConvention(modelMetadataProvider),
            // Apply custom IInputFormatter to the request body
            new MyModelBinderConvention()
        };
    }

    public List&amp;lt;IActionModelConvention&amp;gt; ActionModelConventions { get; }

    public int Order =&amp;gt; 0;

    public void OnProvidersExecuted(ApplicationModelProviderContext context)
    {
    }

    public void OnProvidersExecuting(ApplicationModelProviderContext context)
    {
        foreach (var controller in context.Result.Controllers)
        {
            // apply conventions to all actions if attributed with [MyController]
            if (IsMyController(controller))
                foreach (var action in controller.Actions)
                    foreach (var convention in ActionModelConventions)
                        convention.Apply(action);
        }
    }

    // returns true if the controller is attributed with [MyController]
    private bool IsMyController(ControllerModel controller)
        =&amp;gt; controller.Attributes.OfType&amp;lt;MyControllerAttribute&amp;gt;().Any();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the custom convention:&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;
public class MyModelBinderConvention : IActionModelConvention
{
    public void Apply(ActionModel action)
    {
        foreach (var p in action.Parameters
            // the InferParameterBindingInfoConvention must execute first,
            // which assigns this BindingSource, so if that is assigned
            // we can then assign a custom BinderType to be used.
            .Where(p =&amp;gt; p.BindingInfo?.BindingSource == BindingSource.Body))
        {
            p.BindingInfo.BinderType = typeof(MyModelBinder);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Based on the above application model conventions, any controller attributed with our custom &lt;em&gt;[MyController]&lt;/em&gt; attribute will have these conventions applied to all of it’s actions. With the above, any complex model that will be bound from the request body will use the &lt;em&gt;IModelBinder&lt;/em&gt; type: &lt;em&gt;MyModelBinder, &lt;/em&gt;so here’s how that implementation could look:&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;
// inherit from BodyModelBinder - it does a bunch of magic like caching
// that we don't want to miss out on
public class MyModelBinder : BodyModelBinder
{
    // TODO: You can inject other dependencies to pass to GetInputFormatter
    public MyModelBinder(IHttpRequestStreamReaderFactory readerFactory)
        : base(GetInputFormatter(), readerFactory)
    {
    }

    private static IInputFormatter[] GetInputFormatter()
    {  
        return new IInputFormatter[]
        {
            // TODO: Return any IInputFormatter you want
            new MyInputFormatter()
        };
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The last thing to do is wire it up in DI:&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;
services.TryAddSingleton&amp;lt;MyModelBinder&amp;gt;();            
services.TryAddEnumerable(
    ServiceDescriptor.Transient&amp;lt;IApplicationModelProvider,
    MyApplicationModelProvider&amp;gt;());
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s a reasonable amount of plumbing!&lt;/p&gt;
&lt;p&gt;It could certainly be simpler to configure a body model binder at the controller level but at least there’s actually a way to do it. For a single controller this is quite a lot of work but for a lot of controllers the MVC “application mode” is quite brilliant! … it just took a lot of source code reading to figure that out :)&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:09:49 Z</pubDate>
      <a10:updated>2023-03-23T15:09:49Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1222</guid>
      <link>https://shazwazza.com/post/how-to-register-mvc-controllers-shipped-with-a-class-library-in-aspnet-core/</link>
      <category>ASP.Net</category>
      <title>How to register MVC controllers shipped with a class library in ASP.NET Core</title>
      <description>&lt;p&gt;In many cases you’ll want to ship MVC controllers, possibly views or taghelpers, etc… as part of your class library. To do this correctly you’ll want to add your assembly to ASP.NET’s “Application Parts” on startup. Its quite simple to do but you might want to make sure you are not enabling all sorts of services that the user of your library doesn’t need.&lt;/p&gt;
&lt;p&gt;The common way to do this on startup is to have your own extension method to “Add” your class library to the services. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;public static class MyLibStartup
{
    public static IServiceCollection AddMyLib(this IServiceCollection services)
    {
        //TODO: Add your own custom services to DI

        //Add your assembly to the ASP.NET application parts
        var builder = services.AddMvc();
        builder.AddApplicationPart(typeof(MyLibStartup).Assembly);
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will work, but the call to &lt;a rel="noopener" href="https://github.com/aspnet/Mvc/blob/master/src/Microsoft.AspNetCore.Mvc/MvcServiceCollectionExtensions.cs#L26" target="_blank"&gt;AddMvc() is doing a lot more than you might think&lt;/a&gt; &lt;em&gt;(also note in &lt;/em&gt;&lt;a rel="noopener" href="https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc/src/MvcServiceCollectionExtensions.cs#L27" target="_blank"&gt;&lt;em&gt;ASP.NET Core 3, it’s doing a similar amount&lt;/em&gt;&lt;/a&gt;&lt;em&gt; of work). &lt;/em&gt;This call is adding all of the services to the application required for: authorization, controllers, views, taghelpers, razor, api explorer, CORS, and more… This might be fine if your library requires all of these things but otherwise unless the user of your library also wants all of these things, in my opinion it’s probably better to only automatically add the services that you know your library needs.&lt;/p&gt;
&lt;p&gt;In order to add your assembly application part you need a reference to &lt;em&gt;IMvcBuilder&lt;/em&gt; which you can resolve by calling any number of the extension methods to add the services you need. Depending on what your application requires will depend on what services you’ll want to add. It’s probably best to start with the lowest common feature-set which is a call to AddMvcCore(), the updated code might look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;//Add your assembly to the ASP.NET application parts
var builder = services.AddMvcCore();
builder.AddApplicationPart(typeof(MyLibStartup).Assembly);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;From there you can add the other bits you need, for example, maybe you also need CORS:&lt;/p&gt;
&lt;pre&gt;&lt;code class="lang-csharp"&gt;//Add your assembly to the ASP.NET application parts
var builder = services.AddMvcCore().AddCors();
builder.AddApplicationPart(typeof(MyLibStartup).Assembly);&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:09:29 Z</pubDate>
      <a10:updated>2023-03-23T15:09:29Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1285</guid>
      <link>https://shazwazza.com/post/need-to-remove-an-auto-routed-controller-in-umbraco/</link>
      <category>Umbraco</category>
      <title>Need to remove an auto-routed controller in Umbraco?</title>
      <description>&lt;p&gt;Umbraco will auto-route some controllers automatically. These controllers are any MVC &lt;em&gt;SurfaceControllers&lt;/em&gt; or WebApi &lt;em&gt;UmbracoApiController &lt;/em&gt;types discovered during startup. There might be some cases where you just don’t want these controllers to be routed at all, maybe a package installs a controller that you’d rather not have routable or maybe you want to control if your own plugin controllers are auto-routed based on some configuration.&lt;/p&gt;&lt;p&gt;The good news is that this is quite easy by just removing these routes during startup. There’s various ways you could do this but I’ve shown below one of the ways to interrogate the routes that have been created to remove the ones you don’t want:&lt;/p&gt;&lt;h3&gt;Version 8&lt;/h3&gt;




&lt;pre class="lang-csharp"&gt;&lt;code&gt;
//This is required to ensure this composer runs after
//Umbraco's WebFinalComposer which is the component
//that creates all of the routes during startup    
[ComposeAfter(typeof(WebFinalComposer))]
public class MyComposer : ComponentComposer&amp;lt;MyComponent&amp;gt;{ }

//The component that runs after WebFinalComponent
//during startup to modify the routes
public class MyComponent : IComponent
{
    public void Initialize()
    {
        //list the routes you want removed, in this example
        //this will remove the built in Umbraco UmbRegisterController
        //and the TagsController from being routed.
        var removeRoutes = new[]
        {
            "/surface/umbregister",
            "/api/tags"
        };

        foreach (var route in RouteTable.Routes.OfType&lt;route&gt;().ToList())
        {
            if (removeRoutes.Any(r =&amp;gt; route.Url.InvariantContains(r)))
                RouteTable.Routes.Remove(route);
        }
    }

    public void Terminate() { }
}&lt;/route&gt;&lt;/code&gt;
&lt;/pre&gt;





&lt;h3&gt;Version 7&lt;/h3&gt;

&lt;pre class="lang-csharp"&gt;&lt;code&gt;public class MyStartupHandler : ApplicationEventHandler
{
    protected override void ApplicationStarted(
        UmbracoApplicationBase umbracoApplication,
        ApplicationContext applicationContext)
    {

        //list the routes you want removed, in this example
        //this will remove the built in Umbraco UmbRegisterController
        //and the TagsController from being routed.
        var removeRoutes = new[]
        {
            "/surface/umbregister",
            "/api/tags"
        };

        foreach(var route in RouteTable.Routes.OfType&amp;lt;Route&amp;gt;().ToList())
        {
            if (removeRoutes.Any(r =&amp;gt; route.Url.InvariantContains(r)))
                RouteTable.Routes.Remove(route);
        }
    }
}
&lt;/code&gt;
&lt;/pre&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:09:21 Z</pubDate>
      <a10:updated>2023-03-23T15:09:21Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1324</guid>
      <link>https://shazwazza.com/post/clientdependency-18-released/</link>
      <category>Client Dependency</category>
      <title>ClientDependency 1.8 released</title>
      <description>&lt;p&gt;It’s taken me forever to get this release out purely due to not having enough time, but here it finally is. This update now multi-targets framework versions:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Core project now targets both .Net 4 and 4.5  &lt;li&gt;MVC project is now targets both .Net 4 and 4.5 for MVC 4 and .Net 4.5 for MVC 5&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;There are also a couple of minor bug fixes:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a title="https://github.com/Shandem/ClientDependency/issues/33" href="https://github.com/Shandem/ClientDependency/issues/33"&gt;https://github.com/Shandem/ClientDependency/issues/33&lt;/a&gt;  &lt;li&gt;&lt;a title="https://github.com/Shandem/ClientDependency/issues/34" href="https://github.com/Shandem/ClientDependency/issues/34"&gt;https://github.com/Shandem/ClientDependency/issues/34&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The update to the CDF .Less project is the update to use the latest .Less 1.4.0.0 version.&lt;/p&gt; &lt;p&gt;To install the CDF core:&lt;/p&gt; &lt;style&gt;
.nuget-badge code {
background-color: #202020;
border: 4px solid silver;
border-bottom-left-radius: 5px 5px;
border-bottom-right-radius: 5px 5px;
border-top-left-radius: 5px 5px;
border-top-right-radius: 5px 5px;
color: #e2e2e2;
display: block;
font: normal normal normal 1.5em/normal 'andale mono', 'lucida console', monospace;
line-height: 1.5em;
overflow: auto;
padding: 15px;
}
&lt;/style&gt;  &lt;div class="nuget-badge"&gt; &lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package ClientDependency&lt;/code&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;To install CDF for MVC (less than v5):&lt;/p&gt; &lt;div class="nuget-badge"&gt; &lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package ClientDependency-MVC &lt;/code&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;If you are using MVC 5 then you’ll need to use the MVC 5 specific version:&lt;/p&gt; &lt;div class="nuget-badge"&gt; &lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package ClientDependency-MVC5 &lt;/code&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;To install the .Less update:&lt;/p&gt; &lt;div class="nuget-badge"&gt; &lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package ClientDependency-Less &lt;/code&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Remember CDF also supports TypeScript, CoffeeScript and SASS!&lt;/p&gt; &lt;div class="nuget-badge"&gt; &lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package ClientDependency-TypeScript &lt;/code&gt;&lt;/p&gt;&lt;/div&gt; &lt;div class="nuget-badge"&gt; &lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package ClientDependency-CoffeeScript &lt;/code&gt;&lt;/p&gt;&lt;/div&gt; &lt;div class="nuget-badge"&gt; &lt;p&gt;&lt;code&gt;PM&amp;gt; Install-Package ClientDependency-SASS &lt;/code&gt;&lt;/p&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1184</guid>
      <link>https://shazwazza.com/post/model-binding-with-fromservices-in-aspnet-5/</link>
      <category>ASP.Net</category>
      <title>Model binding with FromServices in ASP.Net 5</title>
      <description>&lt;p&gt;Here’s a new nifty feature I found in ASP.Net 5 – you can construct your model during model binding with IoC without any additional work. This is available on the dev branch on GitHub and is based on something called &lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Core/ModelBinders/ServicesModelBinder.cs" target="_blank"&gt;&lt;em&gt;ServicesModelBinder&lt;/em&gt;&lt;/a&gt;. This is actually pretty cool because it means that you can have a model wired up with all of it’s dependencies based on IoC and then bound to your controller action’s parameters.&lt;/p&gt; &lt;h2&gt;FromServices attribute&lt;/h2&gt; &lt;p&gt;There’s a new attribute called &lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/FromServicesAttribute.cs" target="_blank"&gt;&lt;em&gt;FromServices&lt;/em&gt;&lt;/a&gt; which is what you use to enable this functionality. For example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; async Task&amp;lt;ActionResult&amp;gt; GetProduct(
    [FromServices]ProductModel product)
{

}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;What this attribute does is tell MVC to bind the model using &lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/IServiceActivatorBinderMetadata.cs" target="_blank"&gt;&lt;em&gt;IServiceActivatorBinderMetadata&lt;/em&gt;&lt;/a&gt; which is what the &lt;em&gt;FromServices&lt;/em&gt; attribute implements. This in turn tells MVC to lookup the model binder that is aware of &lt;em&gt;IServiceActivatorBinderMetadata &lt;/em&gt;which happens to be the &lt;em&gt;ServicesModelBinder.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;ServicesModelBinder&lt;/h2&gt;
&lt;p&gt;This model binder is pretty simple, it’s just going to resolve the model type from your IoC container. That could be pretty useful if you need to build up your model properties based on other services. I think some people might argue that this isn’t great practice because this is putting the binding logic in to the model itself instead of using a separate class to perform the binding logic. I suppose it’s up to the individual developer as to what their preference is. You can of course still create your own &lt;em&gt;&lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/Binders/IModelBinder.cs" target="_blank"&gt;IModelBinder&lt;/a&gt;&lt;/em&gt; and use the &lt;em&gt;&lt;a href="https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.ModelBinding/BinderMetadata/ModelBinderAttribute.cs" target="_blank"&gt;ModelBinderAttribute&lt;/a&gt;&lt;/em&gt; to keep the model binding logic in the binder itself.&lt;/p&gt;
&lt;h2&gt;Incoming route values&lt;/h2&gt;
&lt;p&gt;Since the model is being created from IoC, how do you get the current route values to build up your model? To do that you’d put a constructor dependency on &lt;em&gt;IContextAccessor&amp;lt;ActionContext&amp;gt;. &lt;/em&gt;This will give you all of the current route values, &lt;em&gt;HttpContext&lt;/em&gt;, etc… basically everything you’d need to pull the data out of the current request to build your model. &lt;/p&gt;
&lt;h2&gt;Example&lt;/h2&gt;
&lt;p&gt;Given the above GetProduct example, the &lt;em&gt;ProductModel&lt;/em&gt; class could look like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ProductModel
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ProductModel(IContextAccessor&amp;lt;ActionContext&amp;gt; action, IProductService prodService)
    {
        &lt;span class="rem"&gt;//TODO: Do some error checking...&lt;/span&gt;
        var productId = action.Value.RouteData.Values[&lt;span class="str"&gt;"product"&lt;/span&gt;];
        Value = prodService.Get(productId);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; IProduct Value { get; &lt;span class="kwrd"&gt;private&lt;/span&gt; set; }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;This is pretty simple – the &lt;em&gt;IProduct&lt;/em&gt; is looked up from the &lt;em&gt;IProductService&lt;/em&gt; based on the incoming ‘product’ route value. Then in the controller you could just do: &lt;em&gt;product.Value&lt;/em&gt; to get the value for &lt;em&gt;IProduct&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;You then need to ensure that both IProductService and ProductModel are registered as services in your container and it’s really important that the &lt;em&gt;ProductModel&lt;/em&gt; is registered as a Transient object &lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1191</guid>
      <link>https://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-2/</link>
      <category>ASP.Net</category>
      <title>ASP.Net 5 Re-learning a few things (part 2)</title>
      <description>&lt;p&gt;This is part 2 of a series of posts about some fundamental changes in ASP.Net 5 that we’ll need to re-learn (or un-learn!)&lt;/p&gt; &lt;p&gt;Part 1: &lt;a title="http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/" href="http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/"&gt;http://shazwazza.com/post/aspnet-5-re-learning-a-few-things-part-1/&lt;/a&gt;&lt;/p&gt; &lt;h2&gt;System.Web&lt;/h2&gt; &lt;p&gt;This probably isn’t new news to most people since it’s really one of the fundamental shifts for ASP.Net 5 – There won’t be a System.Web DLL. Everything that you’ll install in your website will come as different, smaller, separate libraries. For example, if you want to serve static files, you’d reference the &lt;em&gt;Microsoft.AspNet.StaticFiles&lt;/em&gt; package, if you want to use MVC, you’d include the &lt;em&gt;Microsoft.AspNet.Mvc &lt;/em&gt;package&lt;em&gt;.&lt;/em&gt; &lt;/p&gt; &lt;p&gt;ASP.Net 5: “&lt;a href="http://www.asp.net/vnext/overview/aspnet-vnext/aspnet-5-overview" target="_blank"&gt;consists of modular components with minimal overhead, so you retain flexibility while constructing your solutions&lt;/a&gt;”&lt;/p&gt; &lt;h2&gt;Web Forms&lt;/h2&gt; &lt;p&gt;Gone! YAY! :-)&lt;/p&gt; &lt;p&gt;Web Forms will still be a part of .Net as part of the main framework in System.Web, just not part of ASP.Net 5. &lt;/p&gt; &lt;h2&gt;HttpModules &lt;/h2&gt; &lt;p&gt;An HttpModule simply doesn’t exist in Asp.Net 5, but of course there is a new/better way to achieve this functionality, it’s called Middleware. In an HttpModule, you had to execute code based on the various &lt;a href="http://msdn.microsoft.com/en-us/library/bb470252.aspx" target="_blank"&gt;stages&lt;/a&gt; of a request, things such as &lt;em&gt;AuthenticateRequest, AuthorizeRequest, PostResolveRequestCache&lt;/em&gt;, and other slightly confusingly named events. This is no longer the case with Middleware, things just make sense now … everything is simply a linear execution of your code. You can have multiple middleware’s defined to execute in your application and each one is registered explicitly in your Startup.cs file. As a developer, you are in full control of what get’s executed and in what order instead of not knowing which HttpModules are executing and not really in control of their order of execution. Middleware can simply modify a request and continue calling the next one in the chain, or it can just terminate the pipeline and return a result. &lt;/p&gt; &lt;p&gt;There’s loads of examples in the source for middleware, ranging from the &lt;a href="https://github.com/aspnet/StaticFiles/blob/dev/src/Microsoft.AspNet.StaticFiles/StaticFileMiddleware.cs" target="_blank"&gt;static file middleware&lt;/a&gt; to &lt;a href="https://github.com/aspnet/Security/blob/master/src/Microsoft.AspNet.Security.Cookies/CookieAuthenticationMiddleware.cs" target="_blank"&gt;cookie authentication middleware&lt;/a&gt;,&amp;nbsp; etc…&lt;/p&gt; &lt;p&gt;And &lt;a href="http://whereslou.com/2014/05/28/asp-net-moving-parts-ibuilder/" target="_blank"&gt;here’s a good article&lt;/a&gt; that explains middeware registration and the flow of control.&lt;/p&gt; &lt;h2&gt;HttpHandlers&lt;/h2&gt; &lt;p&gt;HttpHandlers are also a thing of the past. All they really were was a request handler that was based on a specific request path. MVC (which now also includes WebApi) has got this covered. If you really wanted, you could create middleware for this type of functionality as well but unless you require something extraordinarily complex that MVC cannot do (and it can do a lot!), I’d recommend just sticking with MVC.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:15 Z</pubDate>
      <a10:updated>2023-03-23T15:08:15Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1288</guid>
      <link>https://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/</link>
      <category>Umbraco</category>
      <category>ASP.Net MVC</category>
      <title>Custom MVC routes within the Umbraco pipeline</title>
      <description>&lt;p&gt;A while ago I wrote a &lt;a href="http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco" target="_blank"&gt;post on how to do custom MVC routing in Umbraco&lt;/a&gt;, though the end result wasn’t quite ideal. There were a few tricks required and It wasn’t perfect since there were problems with rendering macros on the resulting view, etc… This was due to not having a &lt;em&gt;PublishedContentRequest&lt;/em&gt; object assigned to the context. So then we went ahead and created a new attribute to assign to your MVC action to resolve this: [&lt;em&gt;EnsurePublishedContentRequestAttribute&lt;/em&gt;]&lt;/p&gt; &lt;p&gt;Like the last post, you can read a lot about all of this in &lt;a href="http://our.umbraco.org/forum/developers/extending-umbraco/41367-Umbraco-6-MVC-Custom-MVC-Route?p=3" target="_blank"&gt;this Our thread&lt;/a&gt;. With the [&lt;em&gt;EnsurePublishedContentRequestAttribute&lt;/em&gt;] attribute you could now assign any &lt;em&gt;IPublishedContent&lt;/em&gt; instance to a &lt;em&gt;PublishedContentRequest&lt;/em&gt; and be sure that it was assigned to the &lt;em&gt;UmbracoContext&lt;/em&gt;. But this still isn’t the most ideal way to go about specifying MVC routes to work within the Umbraco pipeline… so I’ve created the following implementation which works quite well.&lt;/p&gt; &lt;h2&gt;Background&lt;/h2&gt; &lt;p&gt;A little bit of background in to custom MVC routes and Umbraco… The reason why it is not terribly straight forward to create a custom route and have it assigned to an Umbraco node is because the node doesn’t exist at your custom route’s location.&lt;/p&gt; &lt;p&gt;For example, if we have this route:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//Create a custom route&lt;/span&gt;
RouteTable.Routes.MapRoute(
    &lt;span class="str"&gt;"test"&lt;/span&gt;,
    &lt;span class="str"&gt;"Products/{action}/{sku}"&lt;/span&gt;,
    &lt;span class="kwrd"&gt;new&lt;/span&gt;
        {
            controller = &lt;span class="str"&gt;"MyProduct"&lt;/span&gt;, 
            action = &lt;span class="str"&gt;"Product"&lt;/span&gt;, 
            sku = UrlParameter.Optional
        });&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Umbraco by default would have no idea what node (&lt;em&gt;IPublishedContent&lt;/em&gt;) would be assigned to this. The way Umbraco relates a URL to an &lt;em&gt;IPublishedContent&lt;/em&gt; instance is by a list of &lt;em&gt;IContentFinder&lt;/em&gt;’s. A very easy way to relate a custom URL to an &lt;em&gt;IPublishedContent&lt;/em&gt; instance is to create your own &lt;a href="http://our.umbraco.org/documentation/Reference/Request-Pipeline/IContentFinder" target="_blank"&gt;IContentFinder&lt;/a&gt;. Combine that with &lt;a href="http://our.umbraco.org/documentation/Reference/Mvc/custom-controllers" target="_blank"&gt;route hijacking&lt;/a&gt; and in many cases this would probably be enough for your custom routing needs. However, it does not solve how you would wire up custom route parameters to your controller like how MVC normally works. Like in the above routing example, you’d want to have the ‘sku’ parameter value wired up to your Action parameter.&lt;/p&gt;
&lt;p&gt;The above route can work and be integrated into Umbraco by following some aspects of my &lt;a href="http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco" target="_blank"&gt;previous blog post&lt;/a&gt; and use the [&lt;em&gt;EnsurePublishedContentRequestAttribute], &lt;/em&gt;but we can make it easier…&lt;/p&gt;
&lt;h2&gt;Creating routes&lt;/h2&gt;
&lt;p&gt;The simplest way to demonstrate this new way to create MVC routes in Umbraco is to just show you an example, so here it is:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;//custom route&lt;/span&gt;
routes.MapUmbracoRoute(
    &lt;span class="str"&gt;"test"&lt;/span&gt;,
    &lt;span class="str"&gt;"Products/{action}/{sku}"&lt;/span&gt;,
    &lt;span class="kwrd"&gt;new&lt;/span&gt;
    {
        controller = &lt;span class="str"&gt;"MyProduct"&lt;/span&gt;,
        sku = UrlParameter.Optional
    },
    &lt;span class="kwrd"&gt;new&lt;/span&gt; ProductsRouteHandler(_productsNodeId));&lt;/pre&gt;
&lt;p&gt;This is using a new extension method: &lt;em&gt;MapUmbracoRoute &lt;/em&gt;which takes in the normal routing parameters (you can also include constraints, namespaces, etc….) but also takes in an instance of &lt;em&gt;UmbracoVirtualNodeRouteHandler. &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The instance of UmbracoVirtualNodeRouteHandler is responsible for associating an &lt;em&gt;IPublishedContent &lt;/em&gt;with this route. It has one abstract method which must be implemented:&lt;/p&gt;&lt;pre class="csharpcode"&gt;IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)&lt;/pre&gt;
&lt;p&gt;It has another virtual method that can be overridden which will allow you to manipulate the &lt;em&gt;PublishedContentRequest &lt;/em&gt;however you’d like:&lt;/p&gt;&lt;pre class="csharpcode"&gt;PreparePublishedContentRequest(PublishedContentRequest publishedContentRequest)&lt;/pre&gt;
&lt;p&gt;So how do you find content to associate with the route? Well that’s up to you, one way (as seen above) would be to specify a node Id. In the example my ProductsRouteHandler is inheriting from &lt;em&gt;UmbracoVirtualNodeByIdRouteHandler &lt;/em&gt;which has an abstract method:&lt;/p&gt;&lt;pre class="csharpcode"&gt;IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext, 
    IPublishedContent baseContent);&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
So based on all this information provided in these methods, you can associate whatever IPublishedContent item you want to the request.&lt;/p&gt;
&lt;h2&gt;Virtual content&lt;/h2&gt;
&lt;p&gt;This implementation expects &lt;strong&gt;any&lt;/strong&gt; instance of IPublishedContent, so this means you can create your own virtual nodes with any custom properties you want. Generally speaking you’ll probably have a real Umbraco IPublishedContent instance as a reference point, so you could create your own virtual IPublishedContent item based on &lt;em&gt;PublishedContentWrapped&lt;/em&gt;, pass in this real node and then just override whatever properties you want, like the page Name, etc..&lt;/p&gt;
&lt;p&gt;Whatever instance of IPublishedContent returned here will be converted to a &lt;em&gt;RenderModel&lt;/em&gt; for use in your controllers.&lt;/p&gt;
&lt;h2&gt;Controllers&lt;/h2&gt;
&lt;p&gt;Controllers are straight forward and work like any other routed controller except that the Action will have an instance of RenderModel mapped to it’s parameter. Example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyProductController : RenderMvcController
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Product(RenderModel model, &lt;span class="kwrd"&gt;string&lt;/span&gt; sku)
    {
        &lt;span class="rem"&gt;//in my case, the IPublishedContent attached to this&lt;/span&gt;
        &lt;span class="rem"&gt;// model will be my products node in Umbraco which i &lt;/span&gt;
        &lt;span class="rem"&gt;// can now use to traverse to display the product list&lt;/span&gt;
        &lt;span class="rem"&gt;// or lookup the product by sku&lt;/span&gt;
            
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(sku))
        {
            &lt;span class="rem"&gt;//render the products list if no sku&lt;/span&gt;
            &lt;span class="kwrd"&gt;return&lt;/span&gt; RenderProductsList(model);
        }
        &lt;span class="kwrd"&gt;else&lt;/span&gt;
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; RenderProduct(model, sku);
        }
    }
}&lt;/pre&gt;
&lt;p&gt;I have this all working well in a side project of mine at the moment. This functionality will be exposed in an upcoming Umbraco version near you&amp;nbsp; :)&lt;/p&gt;
&lt;p&gt;It’s also worth noting that all of this was accomplished outside of the Umbraco core with the publicly available APIs that currently exist. I will admit though there were a few hacks involved which of course won’t be hacks when moved into the core ;)&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:14 Z</pubDate>
      <a10:updated>2023-03-23T15:08:14Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1318</guid>
      <link>https://shazwazza.com/post/mvc-controllers-as-plugins-with-mef-and-autofac/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>MVC Controllers as plugins with MEF and Autofac</title>
      <description>&lt;h2&gt;Disclaimer&lt;/h2&gt;  &lt;p&gt;&lt;em&gt;This blog posts talks about &lt;/em&gt;&lt;a href="http://umbraco.org" target="_blank"&gt;&lt;em&gt;Umbraco&lt;/em&gt;&lt;/a&gt;&lt;em&gt; v5 (Jupiter), however since the state of the codebase is in it’s infancy, the details about Umbraco v5 in this article may not be accurate once Umbraco v5 is released.&lt;/em&gt;&lt;/p&gt;  &lt;h2&gt;Plugins&lt;/h2&gt;  &lt;p&gt;In Umbraco v5 we’re introducing the concept of plugins. Currently in Umbraco, we have the notion of plugins such as Trees, Actions (used on context menus), Data types, etc… Each of these objects is found using a class called TypeFinder to search for specific types in all assemblies that are loaded in the ‘bin’ folder. Though this works, it is definitely not the most efficient approach to plugins. For Umbraco v5, we’re now using &lt;a href="http://mef.codeplex.com/" target="_blank"&gt;MEF&lt;/a&gt; as the plugin framework which is now part of .Net framework 4.&amp;#160; There will be many coming blog posts about the different types of plugins, how they work and how to create them using MEF, but this blog post is about overcoming a small hurdle we encountered when trying to integrate MEF, Autofac and MVC controllers as 3rd party plugins….&lt;/p&gt;  &lt;h2&gt;MVC Controllers as plugins&lt;/h2&gt;  &lt;p&gt;For Umbraco v5, we’re using &lt;a href="http://code.google.com/p/autofac/" target="_blank"&gt;Autofac&lt;/a&gt; as the &lt;a href="http://martinfowler.com/articles/injection.html" target="_blank"&gt;IoC&lt;/a&gt; container which works really well for injecting dependencies into MVC controllers (amongst a ton of other things). Autofac also plays really nicely with MEF through it’s &lt;a href="http://code.google.com/p/autofac/wiki/MefIntegration" target="_blank"&gt;MEF integration&lt;/a&gt; library and requires all of 2 lines of code to get the MEF plugins into our IoC container:&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:42f265e7-c968-4ff1-a6d7-aef30243b292" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 551px; height: 58px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;var catalog &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; DirectoryCatalog(
    m_HttpServer.MapPath(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;~/Plugins/Trees&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
builder.RegisterComposablePartCatalog(catalog);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;The above code tells Autofac to register all &lt;a href="http://mef.codeplex.com/wikipage?title=Declaring%20Exports&amp;amp;referringTitle=Overview" target="_blank"&gt;exportable&lt;/a&gt; MEF components that are found in all assemblies in the &lt;em&gt;~/Plugins/Trees&lt;/em&gt; folder. In our case, the plugins in this folder are actually MVC Controllers which will get registered in to the IoC container and since we are currently using the &lt;a href="http://code.google.com/p/autofac/wiki/MvcIntegration" target="_blank"&gt;AutofacControllerFactory&lt;/a&gt; for our MVC controller factory, it ‘should’ be able to create the requested controller and inject it’s dependencies because we’ve stuck it in the container.&lt;/p&gt;

&lt;h3&gt;Problem 1&lt;/h3&gt;

&lt;p&gt;So far this is all very easy, but as soon as we want to route a request to this controller we end up with a 404 error stating that the page/controller cannot be found.&amp;#160; The reason for this is because the AutofacControllerFactory inherits from the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultcontrollerfactory%28VS.98%29.aspx" target="_blank"&gt;DefaultControllerFactory&lt;/a&gt; which searches for controllers using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.compilation.buildmanager.aspx" target="_blank"&gt;BuildManager&lt;/a&gt; which only searches for types registered in the ‘bin’ folder. I was hoping that the AutofacControllerFactory would have also searched within it’s registrations but this turns out to not be the case. (In fact, once Autofac releases it’s MVC3 implementation, it will no longer be shipped with an AutofacControllerFactory and instead be using MVC3’s new DependencyResolver).&lt;/p&gt;

&lt;h3&gt;Solution 1&lt;/h3&gt;

&lt;p&gt;In order for this to work we have to create our own ControllerFactory&amp;#160; (source code at the end of this article) and provide our own implementation for the methods:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:7930359f-54bd-4536-b44e-f202b3b6ac6a" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 43px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;IController GetControllerInstance(RequestContext context, Type controllerType)
Type GetControllerType(RequestContext requestContext, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; controllerName)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The GetControllerType implementation does the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Checks if the requested controllerName has already been resolved and exists in our factory’s internal cache&lt;/li&gt;

  &lt;li&gt;If not, try to find the controller by name from the underlying DefaultControllerFactory&lt;/li&gt;

  &lt;li&gt;If not found, search the IoC container’s registrations for the type, &lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;if found, store the reference Type in the factory’s internal cache&lt;/li&gt;
  &lt;/ul&gt;
&lt;/ul&gt;

&lt;p&gt;The GetControllerInstance does the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Check if the controllerType has been registered in the factory’s internal cache&lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;If so, get the return the resolved IController instance from the container&lt;/li&gt;
  &lt;/ul&gt;

  &lt;li&gt;If not, try to create the controller from the underlying DefaultControllerFactory&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Problem 2&lt;/h3&gt;

&lt;p&gt;The next problem we encountered was that Autofac couldn’t inject dependent objects into the controllers that weren’t registered in the container using the Autofac MEF extensions. A quick &lt;a href="http://groups.google.com/group/autofac/browse_thread/thread/cf73b3bb80469f1" target="_blank"&gt;post&lt;/a&gt; on to the Autofac Google Group and I had my answer (read the post if you want examples)! &lt;/p&gt;

&lt;h3&gt;Solution 2&lt;/h3&gt;

&lt;p&gt;Essentially, if you want to expose objects to MEF in Autofac, you have to explicitly register them in the container with the ‘Exported’ extension method. Example:&lt;/p&gt;

&lt;p&gt;
  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:2db5c654-2b7e-4343-b579-2a3202ad1d84" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 127px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;register the umbraco settings&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;builder.Register&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;UmbracoSettings&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; UmbracoSettings.GetSettings())
    .As&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IUmbracoSettings&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;()
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;ensure it's registered for MEF too&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    .Exported(x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; x.As&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;IUmbracoSettings&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;())
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;only have one instance ever&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    .SingleInstance(); &lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;h3&gt;Problem 3&lt;/h3&gt;

&lt;p&gt;The last problem was that by default it seems that exported MEF components are instantiated as singletons, meaning that each time you try to resolve a MEF component of a certain type it will always be the exact same instance. MVC really doesn’t like this when it comes to controllers, in fact MVC gives you a very informative error message regarding this and explains that if you are using dependency injection to create controllers to make sure the container is configured to resolve new controller objects, not the same instance.&lt;/p&gt;

&lt;h3&gt;Solution 3&lt;/h3&gt;

&lt;p&gt;All we need to do is add a &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.partcreationpolicyattribute.aspx" target="_blank"&gt;PartCreationPolicyAttribute&lt;/a&gt; to the exported MEF type and set it to CreationPolicy.NonShared which tells the framework to create a new instance each time it is resolved. Example:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:62c66697-6342-44eb-9811-bc9e0002b7eb" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 55px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;[Tree(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(ContentTreeController))]
[PartCreationPolicy(CreationPolicy.NonShared)]
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; ContentTreeController : DemoDataTreeController&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;h2&gt;UmbracoControllerFactory source&lt;/h2&gt;

&lt;p&gt;Here’s the source code for the custom controller factory, it currently inherits from AutofacControllerFactory but once we upgrade to use the new MVC 3 Autofac implementation, this will simply inherit from DefaultControllerFactory.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:e6bd69b7-88a0-4f5c-a698-dd379076a554" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 548px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Web;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Web.Mvc;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Linq;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Web.Routing;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; Autofac;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; Autofac.Core;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; Autofac.Integration.Web;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; Autofac.Integration.Web.Mvc;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Collections.Generic;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; Autofac.Features.Metadata;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; UmbracoProjects.CMS.Web.Trees.Controllers;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Text.RegularExpressions;

&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;namespace&lt;/span&gt;&lt;span style="color: #000000;"&gt; UmbracoProjects.CMS.Web.Mvc.Controllers
{

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; UmbracoControllerFactory : AutofacControllerFactory
    {

        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; IContainer m_Container;

        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; m_Locker &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt;();

        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Used to cache found controllers in the IoC container
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; Dictionary&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;, ServiceTypeCache&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; m_IoCControllerCache &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Dictionary&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;, ServiceTypeCache&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; ServiceTypeCache
        {
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; Service Service { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;set&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; Type ComponentType { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt;; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;set&lt;/span&gt;&lt;span style="color: #000000;"&gt;; }
        }

        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Initializes a new instance of the &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;see cref=&amp;quot;AutofacControllerFactory&amp;quot;/&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt; class.
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name=&amp;quot;containerProvider&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;The container provider.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; UmbracoControllerFactory(IContainerProvider containerProvider)
            : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;base&lt;/span&gt;&lt;span style="color: #000000;"&gt;(containerProvider)
        {
            m_Container &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; containerProvider.ApplicationContainer;
        }

        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Creates the controller based on controller type
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name=&amp;quot;context&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;The context.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name=&amp;quot;controllerType&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;Type of the controller.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;The controller.&lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;remarks&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; This first checks our IoC service internal cache to see if it exists, if it does, we'll try to resolve the controller
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; from IoC, otherwise we'll try to resolve the controller from the underlying factory
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;override&lt;/span&gt;&lt;span style="color: #000000;"&gt; IController GetControllerInstance(RequestContext context, Type controllerType)
        {
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (context &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ArgumentNullException(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;context&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);

            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;first, check if this service by this type is in our cache&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (m_IoCControllerCache.Where(x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; x.Value.ComponentType.Equals(controllerType)).Any())
            {
                var controllerService &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; m_IoCControllerCache.Where(x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; x.Value.ComponentType.Equals(controllerType)).SingleOrDefault().Value.Service;
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; controller;
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (m_Container.TryResolveService(controllerService, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;out&lt;/span&gt;&lt;span style="color: #000000;"&gt; controller))
                {
                    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;if the controller is created by MEF, then resolve&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (controller &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;is&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.ComponentModel.Composition.Primitives.Export)
                    {
                        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; (IController)((System.ComponentModel.Composition.Primitives.Export)controller).Value;
                    }
                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;else&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (controller &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;is&lt;/span&gt;&lt;span style="color: #000000;"&gt; IController)
                    {
                        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; (IController)controller;
                    }                 
                }
                
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; HttpException(&lt;/span&gt;&lt;span style="color: #800080;"&gt;404&lt;/span&gt;&lt;span style="color: #000000;"&gt;,
                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Format(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;Controller type &lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; controllerType &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt; could not be resolved&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;,
                        controllerService,
                        controllerType.FullName,
                        context.HttpContext.Request.Path));
            }

            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;otherwise, try to create from the underlying factory&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;base&lt;/span&gt;&lt;span style="color: #000000;"&gt;.GetControllerInstance(context, controllerType);
        }

        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Finds a controller type based on it's name. 
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name=&amp;quot;requestContext&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;param name=&amp;quot;controllerName&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;remarks&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; This searches using the DefaultControllerFactory's implementation and
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; also searches the IoC container for registered types since types that are registered in the container 
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; may exist outside the bin folder
        &lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;override&lt;/span&gt;&lt;span style="color: #000000;"&gt; Type GetControllerType(RequestContext requestContext, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt; controllerName)
        {
            var controllerKeyName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; controllerName.ToUpper();

            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;first, check if this is already in our internal cache&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (m_IoCControllerCache.ContainsKey(controllerKeyName))
            {
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; m_IoCControllerCache[controllerKeyName].ComponentType;
            }

            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;next, try to resolve it from the underlying factory&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            var type &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;base&lt;/span&gt;&lt;span style="color: #000000;"&gt;.GetControllerType(requestContext, controllerName);

            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;if the controller can't be resolved by name using the standard method, then we should check our container&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (type &lt;/span&gt;&lt;span style="color: #000000;"&gt;==&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            {   
                                
                &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;we need to search the registered components in IoC for a match                &lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt;(var reg &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; m_Container.ComponentRegistry.Registrations) 
                {
                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var s &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; reg.Services)
                    {
                        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;match namespaces/classe that then end with &amp;quot;controllerName + Controller&amp;quot;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (Regex.IsMatch(s.Description, &lt;/span&gt;&lt;span style="color: #800000;"&gt;@&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;(?:\w|\.)+\.&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; controllerName &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;Controller$&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, RegexOptions.Compiled &lt;/span&gt;&lt;span style="color: #000000;"&gt;|&lt;/span&gt;&lt;span style="color: #000000;"&gt; RegexOptions.IgnoreCase))
                        {
                            var controller &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; m_Container.ResolveService(s);
                            var cType &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; controller.GetType();
                            
                            &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;add to the internal cache&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #000000;"&gt;!&lt;/span&gt;&lt;span style="color: #000000;"&gt;m_IoCControllerCache.ContainsKey(controllerKeyName))
                            {
                                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;lock&lt;/span&gt;&lt;span style="color: #000000;"&gt; (m_Locker)
                                {
                                    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;double check&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                                    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #000000;"&gt;!&lt;/span&gt;&lt;span style="color: #000000;"&gt;m_IoCControllerCache.ContainsKey(controllerKeyName))
                                    {
                                        m_IoCControllerCache.Add(controllerKeyName, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ServiceTypeCache() { Service &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; s, ComponentType &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; cType });
                                    }
                                }
                            }
                            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; cType;
                        }
                    }
                }                
            }

            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; type;
        }      
      
    }
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1198</guid>
      <link>https://shazwazza.com/post/binding-a-custom-property-with-model-binders/</link>
      <category>ASP.Net</category>
      <title>Binding a custom property with model binders</title>
      <description>&lt;div class="imported-post"&gt;This post was imported from FARMCode.org which has been discontinued. These posts now exist here as an archive. They may contain broken links and images.&lt;/div&gt;In ASP.Net MVC Model Binders are great but what if you have a property on your model that is a ‘Simple’ type that you want to assign to a custom model binder? Take the following class for example:  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:75dc452e-2b93-4fd6-8263-ad571eaa5e4c" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 523px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; MyModel
{
    [Required]
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; Name { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }

    [Required]
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; DateTime StartDate { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }

    [Required]
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; DateTime StartTime { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }

    [CustomValidation(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(CreateJourneyModel), &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;OnDateValidate&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)]
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; DateTime FullStartDateTime { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }

    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Custom validation logic which needs to validate the full date/time&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; ValidationResult OnDateValidate(DateTime dt, 
        ValidationContext validationContext)
    {
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; dt &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt; DateTime.Now.AddHours(&lt;/span&gt;&lt;span style="color: #800080"&gt;1&lt;/span&gt;&lt;span style="color: #000000"&gt;) 
            &lt;/span&gt;&lt;span style="color: #000000"&gt;?&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; ValidationResult(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;Enter a date/time greater than 3 hours from now&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)
            : ValidationResult.Success;
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;The above model will allow us to have a different date and time field rendered for input on a form but requires the combined date/time to run custom validation against. You might already be thinking: Isn’t this the same problem that &lt;a href="http://www.hanselman.com/" target="_blank"&gt;Scott Hanselman&lt;/a&gt; solved in this &lt;a href="http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx" target="_blank"&gt;post&lt;/a&gt;? Well, sort of but not really. What Scott did was assign a custom model binder to all instances of DateTime or set specifically on an Action parameter and was also splitting one DateTime field into 2 on the front-end . I’m not really a fan of having a custom model binder for all instances of DateTime in my application , I don’t have a DateTime parameter in my Action to explicitly assign a model binder too and I also decided to use 2 DateTime properties to represent 2 different fields on my form.&amp;nbsp; The solution presented below could allow you to custom bind any single property of your model which you could then assign custom validation to. &lt;/p&gt;
&lt;p&gt;First, we need to put the fields on your form so they get posted back to the server. Using the Html.EditorFor(x =&amp;gt; …..) syntax is the way to go, however, for the field we want to custom bind using Html.HiddenFor(x =&amp;gt; …. ) is better so the field isn’t actually displayed on the form. This will add this field name to the ValueProvider when it’s posted to the server. &lt;/p&gt;
&lt;p&gt;Next, create the model binder. Since we cannot assign a model binder to a property of a model, we need to make the binder for the model containing the property. And, since we don’t want to do any custom binding for the rest of the properties, we’ll just inherit from &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.aspx" target="_blank"&gt;DefaultModelBinder&lt;/a&gt; and let it do the work for us. The only thing we have to do is override &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.getpropertyvalue.aspx" target="_blank"&gt;GetPropertyValue&lt;/a&gt; , check for the property that we want to custom bind and bind it!&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:740cf9dc-9fd6-4fec-bd5c-76d485524c66" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 832px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; MyCustomModelBinder : DefaultModelBinder
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;override&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; GetPropertyValue(ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        System.ComponentModel.PropertyDescriptor propertyDescriptor, 
        IModelBinder propertyBinder)
    {
        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;check if it the 'FullStartDateTime' property&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (propertyDescriptor.Name &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;FullStartDateTime&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)
        {
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;try to get the date part&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            var dateValue &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; bindingContext.ValueProvider.GetValue(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;StartDate&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            DateTime date;
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #000000"&gt;!&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.IsNullOrEmpty(dateValue.AttemptedValue) &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000"&gt; 
                DateTime.TryParseExact(dateValue.AttemptedValue, &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;dd/MM/yyyy&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, CultureInfo.InvariantCulture, DateTimeStyles.None, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;out&lt;/span&gt;&lt;span style="color: #000000"&gt; date))
            {
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;try to get the time part&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                var timeValue &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; bindingContext.ValueProvider.GetValue(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;StartTime&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
                DateTime time;
                &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #000000"&gt;!&lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt;.IsNullOrEmpty(timeValue.AttemptedValue) &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000"&gt; 
                    DateTime.TryParseExact(timeValue.AttemptedValue, &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;HH:mm&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, CultureInfo.InvariantCulture, DateTimeStyles.None, &lt;/span&gt;&lt;span style="color: #0000ff"&gt;out&lt;/span&gt;&lt;span style="color: #000000"&gt; time))
                {
                    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;we've got both parts, join them together&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                    var fullDateTime &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second);
                    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; fullDateTime;
                }
            }

            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;could not bind&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;&lt;span style="color: #000000"&gt;;
        }

        &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;let the default model binder do it's thing&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;base&lt;/span&gt;&lt;span style="color: #000000"&gt;.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Last, all we need to do is assign the custom model binder to the custom model which can easily be done by just attributing the model:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d970f6d8-a7b8-4889-aebb-f5f0410ee617" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 600px; height: 54px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[ModelBinder(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(MyCustomModelBinder))]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; MyModel {...}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;That's it! Now all validation is handled as per normal on the custom property.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1211</guid>
      <link>https://shazwazza.com/post/custom-mvc-modelbinder-with-complex-modelsobjectsinterfaces-using-built-in-mvc-validation/</link>
      <category>ASP.Net</category>
      <title>Custom MVC ModelBinder with Complex Models/Objects/Interfaces using built in MVC Validation</title>
      <description>&lt;p&gt;I’ve been creating some cool stuff using &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx" target="_blank"&gt;ASP.Net MVC 3&lt;/a&gt; lately and came across a situation where I’d like to have quite a complex model/object bound to an Action on my Controller based on a set of posted values from a form. In order to do this, a custom &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.modelbinders.aspx" target="_blank"&gt;ModelBinder&lt;/a&gt;&lt;em&gt;&lt;/em&gt; is necessary to collect the data from the posted values, turn it into my custom object, and bind that object to my Action’s parameter. The easy part is to write code to turn the posted values into my custom object and return it, the tricky part is trying to get the in-built back-end MVC validation working for my model… which is currently using &lt;em&gt;&lt;a href="http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs" target="_blank"&gt;DataAnnotations&lt;/a&gt;&lt;/em&gt;. I really didn’t feel like writing my own logic to validate my model against &lt;em&gt;&lt;a href="http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs" target="_blank"&gt;DataAnnotations&lt;/a&gt;&lt;/em&gt; and also didn’t want to write the logic to take into account that &lt;em&gt;&lt;a href="http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs" target="_blank"&gt;DataAnnotations&lt;/a&gt;&lt;/em&gt; might not be the current developers validation provider of choice. So after much digging through the source of MVC and using &lt;a href="http://www.red-gate.com/products/reflector/" target="_blank"&gt;Reflector&lt;/a&gt;, I finally found the solution.&amp;nbsp; To better describe the concept, here’s an example of the issue:&lt;/p&gt; &lt;p&gt;Each &lt;em&gt;IMyObject&lt;/em&gt; has many properties: &lt;em&gt;IProperty&lt;/em&gt;. Each &lt;em&gt;IProperty&lt;/em&gt; is of a type: &lt;em&gt;IType&lt;/em&gt; and each &lt;em&gt;IType&lt;/em&gt; has a model which is used to render out the editor in MVC (&lt;a href="http://msdn.microsoft.com/en-us/library/ee402949.aspx" target="_blank"&gt;EditorFor&lt;/a&gt;&lt;em&gt;&lt;/em&gt;)&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:68eead81-30aa-4103-b787-a53eac690c6c" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 642px; height: 394px; overflow: hidden"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IMyObject
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt; Id { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; Name { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    IEnumerable&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;IProperty&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; Properties { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
}

&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IProperty
{
    Guid Id { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; Alias { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; Name { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;set&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    IType DataType { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
}

&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;interface&lt;/span&gt;&lt;span style="color: #000000"&gt; IType        
{
    Guid Id { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; Name { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&lt;span style="color: #000000"&gt; Alias { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; EditorModel { &lt;/span&gt;&lt;span style="color: #0000ff"&gt;get&lt;/span&gt;&lt;span style="color: #000000"&gt;; }
}

&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;So my controller’s Action looks something like this:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:090cdce2-c708-45ea-86fe-f5ad941c6a2b" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 642px; height: 174px; overflow: hidden"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;override&lt;/span&gt;&lt;span style="color: #000000"&gt; ActionResult Edit(IMyObject obj)
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #000000"&gt;!&lt;/span&gt;&lt;span style="color: #000000"&gt;ModelState.IsValid)
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; View(obj);
 
    &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;Save some data and do other stuff...&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;    
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;Initially my model binder looks like this which is the ‘easy’ part that converts the posted values into an &lt;em&gt;IMyObject&lt;/em&gt; object with all of it’s values filled in:&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:decbd6d0-0b2b-4bd7-8d67-238c678aa4d7" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 642px; height: 658px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[ModelBinderType(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(IMyObject))]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; EditorModelBinder : DefaultModelBinder
{    
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;override&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; CreateModel(ControllerContext controllerContext,         
        ModelBindingContext bindingContext, Type modelType)
    {        
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (modelType.Equals(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(IMyObject)))
        {                
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;get the id from the posted values&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            var id &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;)controllerContext
                .RouteData
                .Values
                .GetRequiredObject(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;id&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;get the object from my data repository by id&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            var model &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; GetMyObjectFromMyDataRepository(id);
            
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (var p &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; item.Properties)
            {
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;it's the editor model that created the editor
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;for each property in mvc which means that
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;it's this data that is being posted back&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                var editorModel &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; p.DataType.EditorModel;
                
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; ... Go convert all of the posted values using the bindingContext
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; ValueProvider and build up the MyObject object created above
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; ... (A bunch of code goes here to do the conversion)
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; Except, now that it's created, how the heck do i run it through
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; MVC validation?&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            }
            
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; model;
        }

        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;base&lt;/span&gt;&lt;span style="color: #000000"&gt;.CreateModel(controllerContext, bindingContext, modelType);
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;In order for the call in your controller to check if your &lt;em&gt;&lt;a href="http://stackoverflow.com/questions/881281/what-is-modelstate-isvalid-valid-for-in-asp-net-mvc-in-nerddinner" target="_blank"&gt;ModelState.IsValid&lt;/a&gt;&lt;/em&gt;, something needs to do the validation of the model and put the validation results inside the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.modelstate.aspx" target="_blank"&gt;ModelState&lt;/a&gt;&lt;em&gt;&lt;/em&gt; object. This of course already exists in the MVC framework and is done with the &lt;em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.aspx" target="_blank"&gt;DefaultModelBinder&lt;/a&gt;&lt;/em&gt;. The &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder.aspx" target="_blank"&gt;DefaultModelBinder&lt;/a&gt;&lt;em&gt; &lt;/em&gt;is pretty smart and can figure out how to automagically parse and transform the posted values into the specified model and also run it through the MVC validators so long as the model is simple enough to figure out. When your model consists of interfaces, it generally can’t do much about it because it doesn’t know how to create your interface. It also has problems when the model is complex and contains sub objects of sub objects (like the &lt;em&gt;IMyObject&lt;/em&gt;). So how do we tap in to this underlying functionality? &lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:0c165d35-45fd-4e53-b4a5-ebec5e0b4016" class="wlWriterSmartContent"&gt;&lt;pre style="background-color: white; width: 642px; height: 842px; overflow: auto"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;[ModelBinderType(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(IMyObject))]
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; EditorModelBinder : DefaultModelBinder
{    
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;override&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; CreateModel(ControllerContext controllerContext,         
        ModelBindingContext bindingContext, Type modelType)
    {        
        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;&lt;span style="color: #000000"&gt; (modelType.Equals(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(IMyObject)))
        {                
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;get the id from the posted values&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            var id &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="color: #000000"&gt;)controllerContext
                .RouteData
                .Values
                .GetRequiredObject(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;id&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;get the object from my data repository by id&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            var model &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; GetMyObjectFromMyDataRepository(id);
            
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (var p &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; item.Properties)
            {
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;it's the editor model that created the editor
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;for each property in mvc which means that
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;it's this data that is being posted back&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                var editorModel &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; p.DataType.EditorModel;
                
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;get a binder for the EditorModel&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                IModelBinder binder &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;&lt;span style="color: #000000"&gt;.Binders
                    .GetBinder(model.GetType());
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;create a new context for it&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                ModelBindingContext customBindingContext &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; ModelBindingContext();
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;get the meta data for it&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                customBindingContext.ModelMetadata &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; ModelMetadataProviders
                    .Current
                    .GetMetadataForType(() &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; model, model.GetType());
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;ensure we use our correct field 'prefix' 
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;(this is optional and depends on if you are using a custom prefix)&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                customBindingContext.ModelName &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; p.Id.ToString();
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;use our existing model state&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                customBindingContext.ModelState &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; bindingContext.ModelState;                
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;use our existing value provider&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                customBindingContext.ValueProvider &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; bindingContext.ValueProvider;
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;do the binding! this will also validate and put the errors into the ModelState for us.&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                model &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; (&lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt;)binder.BindModel(controllerContext, customBindingContext);
            }
            
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; model;
        }

        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;base&lt;/span&gt;&lt;span style="color: #000000"&gt;.CreateModel(controllerContext, bindingContext, modelType);
    }
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;p&gt;The concept above is pretty much how a Controller’s &lt;em&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryupdatemodel.aspx" target="_blank"&gt;TryUpdateModel&lt;/a&gt;&lt;/em&gt; method works and how it does the underlying validation. &lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1214</guid>
      <link>https://shazwazza.com/post/mvc-attributes-readonly-vs-editable-vs-bind-the-defaultmodelbinder/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>MVC Attributes - ReadOnly vs Editable vs Bind &amp; the DefaultModelBinder</title>
      <description>&lt;p&gt;I’ve been learning quite a lot about the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.defaultmodelbinder%28VS.90%29.aspx" target="_blank"&gt;DefaultModelBInder&lt;/a&gt; and it’s internal behaviour because of the complexities involved with some of the model binding in &lt;a href="http://umbraco.org" target="_blank"&gt;Umbraco&lt;/a&gt; v5 and wanted to point out the behaviour of the following attributes when it comes to how the DefaultModelBinder works:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.readonlyattribute.aspx" target="_blank"&gt;ReadOnly&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.editableattribute%28VS.95%29.aspx" target="_blank"&gt;Editable&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.bindattribute%28VS.90%29.aspx" target="_blank"&gt;Bind&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Each of these attributes ‘seems’ like they serve the same purpose by making properties of your model read only, not-bindable, not-editable or vice versa. In my mind they all seem like they are the same but that’s just my opinion, as there could be some other hidden secret in the .Net framework which differentiates between these explicitly. The DefaultModelBinder definitely doesn’t treat each of these the same, in fact the DefaultModelBinder doesn’t check for the Editable attribute on properties. Before the DefaultModelBinder binds all of your model’s properties (If you have a complex model), it creates a new &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.modelbindingcontext%28VS.98%29.aspx" target="_blank"&gt;ModelBindingContext&lt;/a&gt; which contains a new &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.modelbindingcontext.propertyfilter%28v=VS.98%29.aspx" target="_blank"&gt;PropertyFilter&lt;/a&gt; to exclude any properties that have been defined as ReadOnly or not bindable.&lt;/p&gt;  &lt;p&gt;There’s a few internal/private methods of the DefaultModelBinder that do these lookups:&lt;/p&gt;  &lt;p&gt;This one creates the new model binding context and checks if any BindAttributes have been declared for the model, if so, then it creates a new property filter which combines the BindAttribute filter and the standard model binder filter:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:d5af5bc0-bd08-433a-a14d-54ddc238e97f" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 284px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;internal&lt;/span&gt;&lt;span style="color: #000000;"&gt; ModelBindingContext CreateComplexElementalModelBindingContext(ControllerContext controllerContext, ModelBindingContext bindingContext, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;object&lt;/span&gt;&lt;span style="color: #000000;"&gt; model) {
    BindAttribute bindAttr &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (BindAttribute)GetTypeDescriptor(controllerContext, bindingContext).GetAttributes()[&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000;"&gt;(BindAttribute)];
    Predicate&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; newPropertyFilter &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (bindAttr &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
        &lt;/span&gt;&lt;span style="color: #000000;"&gt;?&lt;/span&gt;&lt;span style="color: #000000;"&gt; propertyName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; bindAttr.IsPropertyAllowed(propertyName) &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000;"&gt; bindingContext.PropertyFilter(propertyName)
        : bindingContext.PropertyFilter;

    ModelBindingContext newBindingContext &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ModelBindingContext() {
        ModelMetadata &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ModelMetadataProviders.Current.GetMetadataForType(() &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; model, bindingContext.ModelType),
        ModelName &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bindingContext.ModelName,
        ModelState &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bindingContext.ModelState,
        PropertyFilter &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; newPropertyFilter,
        ValueProvider &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bindingContext.ValueProvider
    };

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; newBindingContext;
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;This method is called to do the actual binding of each property, but as you can see it only binds properties that have been filtered:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:10f6d841-5830-401f-bf28-d279d9165728" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 113px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) {
    IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;PropertyDescriptor&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; properties &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetFilteredModelProperties(controllerContext, bindingContext);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt; (PropertyDescriptor property &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; properties) {
        BindProperty(controllerContext, bindingContext, property);
    }
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The GetFilteredModelProperties method gets all of the unfiltered &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.propertydescriptor.aspx" target="_blank"&gt;PropertyDescriptor&lt;/a&gt;’s of the model, and then calls ShouldUpdateProperty to perform the actual filtering (which passes in the new ModelBindingContext’s PropertyFilter which is now based on the Bind attribute if one was declared on the model):&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:ce316a48-d796-4648-815d-8fa0555a05c2" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 161px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;PropertyDescriptor&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetFilteredModelProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) {
    PropertyDescriptorCollection properties &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; GetModelProperties(controllerContext, bindingContext);
    Predicate&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; propertyFilter &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bindingContext.PropertyFilter;

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; from PropertyDescriptor property &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; properties
           &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; ShouldUpdateProperty(property, propertyFilter)
           select property;
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;The ShouldUpdateProperty method does the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Check if the PropertyDescriptor is read only. This is determined by whether or not the ReadOnly attribute was specified on the model property, &lt;em&gt;the Editable attribute doesn’t seem to affect this value&lt;/em&gt;. The first check also makes a call to CanUpdateReadonlyTypedReference which checks for value types such as guid, int, string, and ensures they are excluded if IsReadOnly is true. &lt;/li&gt;

  &lt;li&gt;Check if the property filter allows the property to be bound which is based on the Bind attribute (if supplied on the model) &lt;/li&gt;
&lt;/ol&gt;



&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:6087e244-f8f0-4c47-b538-109f768cdf7a" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 650px; height: 229px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;bool&lt;/span&gt;&lt;span style="color: #000000;"&gt; ShouldUpdateProperty(PropertyDescriptor property, Predicate&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; propertyFilter) {
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (property.IsReadOnly &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;!&lt;/span&gt;&lt;span style="color: #000000;"&gt;CanUpdateReadonlyTypedReference(property.PropertyType)) {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
    }

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; if this property is rejected by the filter, move on&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #000000;"&gt;!&lt;/span&gt;&lt;span style="color: #000000;"&gt;propertyFilter(property.Name)) {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;false&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
    }

    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; otherwise, allow&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;true&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;



&lt;p&gt;So the result of what can actually be bound in the DefaultModelBinder is:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Any property that doesn’t have &lt;em&gt;[ReadOnly(true)] &lt;/em&gt;specified &lt;/li&gt;

  &lt;li&gt;Any property that is referenced as an &lt;em&gt;Include&lt;/em&gt; using the Bind attribute on the model containing the property: 

    &lt;ul&gt;
      &lt;li&gt;[Bind(Include = “MyProperty”)] &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Any property that is NOT referenced as an &lt;em&gt;Exclude &lt;/em&gt;using the Bind attribute on the model containing the property: 

    &lt;ul&gt;
      &lt;li&gt;[Bind(Exclude = “NotThisProperty”)] &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So it appears that even if you set the an editable attribute as [Editable(false)], this property will still be included for binding.&lt;/p&gt;

&lt;p&gt;The DefaultModelBinder is quite a crazy beast and contains a lot of functionality under the hood. What I’ve mentioned above is based purely on experience and looking at the source code so if you have any feedback or feel that any of this is in error please comment!&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1221</guid>
      <link>https://shazwazza.com/post/developing-a-plugin-framework-in-aspnet-mvc-with-medium-trust/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>Developing a plugin framework in ASP.NET MVC with medium trust</title>
      <description>&lt;p&gt;I’ve recently spent quite a lot of time researching and prototyping different ways to create a plugin engine in ASP.NET MVC3 and primarily finding a nice way to load plugins (DLLs) in from outside of the ‘bin’ folder. Although this post focuses on MVC3, I am sure that the same principles will apply for other MVC versions. &lt;/p&gt; &lt;h1&gt;The Issues&lt;/h1&gt; &lt;p&gt;Loading DLLs from outside of the ‘bin’ folder isn’t really anything new or cutting edge, however when working with MVC this becomes more difficult. This is primarily due to how MVC loads/finds types that it needs to process including controllers, view models (more precisely the generic argument passed to a &lt;a href="http://www.google.com.au/url?sa=t&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CBkQFjAA&amp;amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fes-es%2Flibrary%2Fdd470798.aspx&amp;amp;rct=j&amp;amp;q=ViewPage%20msdn&amp;amp;ei=BLAmTd_lPIeycfL36ZgB&amp;amp;usg=AFQjCNFTkQSor1HJFAMkHw0h3BUiiIuEzg&amp;amp;cad=rja" target="_blank"&gt;ViewPage&lt;/a&gt; or used with the @model declaration in Razor), model binders, etc… MVC is very tied to the &lt;a href="http://www.google.com.au/url?sa=t&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CBUQFjAA&amp;amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.web.compilation.buildmanager.aspx&amp;amp;rct=j&amp;amp;q=BuildManager%20msdn%20asp.net&amp;amp;ei=5LAmTa6nFsn4cbKezYEB&amp;amp;usg=AFQjCNH96gOQZt36Kjh-lIs5CKmWEtPjCA&amp;amp;cad=rja" target="_blank"&gt;BuildManager&lt;/a&gt; which is the mechanism for compiling views, and locating other services such as controllers. By default the BuildManager is only familiar with assembies in the ‘bin’ folder and in the GAC, so if you start putting DLLs in folders outside of the ‘bin’ then it won’t be able to locate the MVC services and objects that you might want it to be referencing.&lt;/p&gt; &lt;p&gt;Another issue that needs to be dealt with is DLL file locking. When a plugin DLL is loaded and is in use the CLR will lock the file. This becomes an an issue if developers want to update the plugin DLL while the website is running since they won’t be able to unless they bump the web.config or take the site down. This holds true for &lt;a href="http://msdn.microsoft.com/en-us/library/dd460648.aspx" target="_blank"&gt;MEF&lt;/a&gt; and how it loads DLLs as well.&lt;/p&gt; &lt;h1&gt;.Net 4 to the rescue… almost&lt;/h1&gt; &lt;p&gt;One of the new features in .Net 4 is the ability to execute code before the app initializes which compliments another new feature of the BuildManager that lets you add assembly references to it at runtime (which must be done on application pre-init). Here’s a nice little reference to these new features from Phil Haack: &lt;a title="http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx" href="http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx"&gt;http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx&lt;/a&gt;.&amp;nbsp; This is essential to making a plugin framework work with MVC so that the BuildManager knows where to reference your plugin DLLs outside of the ‘bin’. However, this isn’t the end of the story. &lt;/p&gt; &lt;h2&gt;Strongly typed Views with model Types located in plugin DLLs&lt;/h2&gt; &lt;p&gt;Unfortunately if you have a view that is strongly typed to a model that exists outside of the ‘bin’, then you’ll find out very quickly that it doesn’t work and it won’t actually tell you why. This is because the &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx" target="_blank"&gt;RazorViewEngine&lt;/a&gt;&amp;nbsp; uses the BuildManager to compile the view into a dynamic assembly but then uses &lt;em&gt;Activator.CreateInstance&lt;/em&gt; to instantiate the newly compiled object. This is where the problem lies, the current AppDomain doesn’t know how to resolve the model Type for the strongly typed view since it doesn’t exist in the ‘bin’ or GAC.&amp;nbsp; An even worse part about this scenario is that you don’t get any error message telling you why this isn’t working, or where the problem is. Instead you get the nice MVC view not found error: “…&lt;i&gt;or its master was not found or no view engine supports the searched locations. The following locations were searched: ….”&lt;/i&gt; telling you that it has searched for views in all of the ViewEngine locations and couldn’t find it… which is actually not the error at all.&amp;nbsp; Deep in the MVC3 source, it tries to instantiate the view object from the dynamic assembly and it fails so it just keeps looking for that view in the rest of the ViewEngine paths.&lt;/p&gt; &lt;p&gt;&lt;em&gt;NOTE: Even though in MVC3 there’s a new &lt;a href="http://bradwilson.typepad.com/blog/2010/10/service-location-pt11-view-page-activator.html" target="_blank"&gt;IViewPageActivator&lt;/a&gt; which should be responsible for instantiating the views that have been compiled with the BuildManager, implementing a custom IViewPageActivator to handle this still does not work because somewhere in the MVC3 codebase fails before the call to the IViewPageActivator which has to do with resolving an Assembly that is not in the ‘bin’. &lt;/em&gt;&lt;/p&gt; &lt;h1&gt;Full trust&lt;/h1&gt; &lt;p&gt;When working in &lt;a href="http://msdn.microsoft.com/en-us/library/wyts434y.aspx" target="_blank"&gt;Full Trust&lt;/a&gt; we have a few options for dealing with the above scenario:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Use the AppDomain’s &lt;a href="http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx" target="_blank"&gt;ResolveAssembly&lt;/a&gt; event  &lt;ul&gt; &lt;li&gt;By subscribing to this event, you are able to instruct the AppDomain where to look when it can’t find a reference to a Type.  &lt;li&gt;This is easily done by checking if your plugin assemblies match the assembly being searched for, and then returning the Assembly object if found: &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:1991111b-18f6-447d-b453-b6a3a4f22a0b" class="wlWriterSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre style="overflow: auto; height: 152px; width: 680px; background-color: white"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; Assembly CurrentDomain_AssemblyResolve(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&lt;span style="color: #000000"&gt; sender, ResolveEventArgs args)
{
    var pluginsFolder &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DirectoryInfo(HostingEnvironment.MapPath(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;~/Plugins&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;));
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;&lt;span style="color: #000000"&gt; (from f &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; pluginsFolder.GetFiles(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;*.dll&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, SearchOption.AllDirectories)
            let assemblyName &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; AssemblyName.GetAssemblyName(f.FullName)
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="color: #000000"&gt; assemblyName.FullName &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; args.Name &lt;/span&gt;&lt;span style="color: #000000"&gt;||&lt;/span&gt;&lt;span style="color: #000000"&gt; assemblyName.FullName.Split(&lt;/span&gt;&lt;span style="color: #800000"&gt;'&lt;/span&gt;&lt;span style="color: #800000"&gt;,&lt;/span&gt;&lt;span style="color: #800000"&gt;'&lt;/span&gt;&lt;span style="color: #000000"&gt;)[&lt;/span&gt;&lt;span style="color: #800080"&gt;0&lt;/span&gt;&lt;span style="color: #000000"&gt;] &lt;/span&gt;&lt;span style="color: #000000"&gt;==&lt;/span&gt;&lt;span style="color: #000000"&gt; args.Name
            select Assembly.LoadFile(f.FullName)).FirstOrDefault();
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Shadow copy your plugin DLLs into the AppDomain’s &lt;a href="http://msdn.microsoft.com/en-us/library/system.appdomain.dynamicdirectory.aspx" target="_blank"&gt;DynamicDirectory&lt;/a&gt;. 
&lt;ul&gt;
&lt;li&gt;This is the directory that the BuildManager compiles it’s dynamic assemblies into and is also a directory that the AppDomain looks to when resolving Type’s from Assemblies. 
&lt;li&gt;You can shadow copy your plugin DLLs to this folder on app pre-init and everything ‘should just work’ &lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Replace the RazorViewEngine with a custom razor view engine that compiles views manually but makes references to the appropriate plugin DLLs 
&lt;ul&gt;
&lt;li&gt;I actually had this working in an &lt;a href="http://umbraco.org" target="_blank"&gt;Umbraco&lt;/a&gt; v5 prototype but it is hugely overkill and unnecessary plus you actually would have to replace the RazorViewEngine which is pretty absurd. &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;h1&gt;The burden of Medium Trust&lt;/h1&gt;
&lt;p&gt;In the MVC world there’s only a couple hurdles to jump when loading in plugins from outside of the ‘bin’ folder in Full Trust. In Medium Trust however, things get interesting. Unfortunately in Medium Trust it is not possible to handle the AssemblyResolve event and it’s also not possible to access the DynamicDirectory of the AppDomain so the above two solutions get thrown out the window. Further to this it seems as though you can’t use CodeDom in Medium Trust to custom compile views.&lt;/p&gt;
&lt;h2&gt;Previous attempts&lt;/h2&gt;
&lt;p&gt;For a while I began to think that this wasn’t possible and I thought I tried everything:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Shadow copying DLLs from the plugins folder into the ‘bin’ folder on application pre-init 
&lt;ul&gt;
&lt;li&gt;This fails because even during app pre-init, the application pool will still recycle. Well, it doesn’t actually ‘fail’ unless you keep re-copying the DLL into the bin. If you check if it already exists and don’t copy into the bin than this solution will work for you but it’s hardly a ‘solution’ since you might as well just put all your DLLs into the ‘bin’ in the first place. &lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Trying to use sub folders of the ‘bin’ folder to load plugins. 
&lt;ul&gt;
&lt;li&gt;Turns out that ASP.Net doesn’t by default load in DLLs that exist in sub folders of the bin, though from research it looks like standard .Net apps actually do. 
&lt;li&gt;Another interesting point was that if you try to copy a DLL into a sub folder of the bin during application pre-init you get a funky error:&amp;nbsp; “Storage scopes cannot be created when _AppStart is executing”. It seems that ASP.Net is monitoring all changes in the bin folder regardless of whether or not they are in sub folders but still doesn’t load or reference those assemblies. &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;h2&gt;An easy solution&lt;/h2&gt;
&lt;p&gt;So, the easy solution is to just set a &lt;a href="http://msdn.microsoft.com/en-us/library/823z9h8w%28vs.71%29.aspx" target="_blank"&gt;‘privatePath’ on the ‘probing’ element&lt;/a&gt; in your web.config to tell the AppDomain to also look for Assemblies/Types in the specified folders. I did try this before when trying to load plugins from sub folders in the bin and couldn’t get it to work. I’m not sure if I was ‘doing it wrong’ but it definitely wasn’t working then, either that or attempting to set this in sub folders of the bin just doesn’t work.&lt;/p&gt;
&lt;div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f5d93897-6e1d-4814-bf75-c9e4c57ad588" class="wlWriterSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre style="overflow: auto; height: 68px; width: 680px; background-color: white"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;runtime&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;
  &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;assemblyBinding xmlns&lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;urn:schemas-microsoft-com:asm.v1&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;       
    &lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000"&gt;probing privatePath&lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;Plugins/temp&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #000000"&gt;/&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;h1&gt;DLL file locking&lt;/h1&gt;
&lt;p&gt;Since plugin DLLs get locked by the CLR when they are loaded, we need to work around this. The solution is to shadow copy the DLLs to another folder on application pre-init. As mentioned previously, this is one of the ways to get plugins loaded in Full Trust and in my opinion is the nicest way to do it since it kills 2 birds with one stone. In Medium Trust however, we’ll have to jump through some hoops and shadow copy the DLLs to a temp folder that exists within the web application. IMPORTANT: When you’re copying DLLs you might be tempted to modify the name of the DLL by adding a version number or similar, but this will NOT work and you’ll get a &lt;em&gt;“The located assembly's manifest definition … does not match the assembly reference.” &lt;/em&gt;exception.&lt;/p&gt;
&lt;h1&gt;Solution&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;&lt;font size="2"&gt;UPDATE: The latest version of this code can be found in the Umbraco v5 source code. The following code does work but there’s been a lot of enhancements to it in the Umbraco core. Here’s the latest changeset as of 16/16/2012 &lt;a href="https://bitbucket.org/Shandem/umbracov5/src/bc972b4dd5f8/Source/Libraries/Umbraco.Cms.Web/System/PluginManager.cs" target="_blank"&gt;Umbraco v5 PluginManager.cs&lt;/a&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Working in Full Trust, the simplest solution is to shadow copy your plugin DLLs into your AppDomain DynamicDirectory. Working in Medium Trust you’ll need to do the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On application pre-init: 
&lt;ul&gt;
&lt;li&gt;Shadow copy all of your plugin DLLs to a temporary folder in your web application (not in the ‘bin’) 
&lt;li&gt;Add all of the copied DLLs to be referenced by the BuildManager &lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Add all folder paths to the &lt;em&gt;privatePath&lt;/em&gt; attribute of the &lt;em&gt;probing&lt;/em&gt; element in your web.config to point to where you will be copying your DLLs 
&lt;ul&gt;
&lt;li&gt;If you have more than one, you need to semi-colon separate them &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Thanks to &lt;a href="http://twitter.com/gblock" target="_blank"&gt;Glenn Block&lt;/a&gt; @ Microsoft who gave me a few suggestions regarding DLL file locking with MEF, Assembly load contexts and probing paths! You put me back on track after I had pretty much given up.&lt;/p&gt;
&lt;p&gt;Here’s the code to do the shadow copying and providing the Assemblies to the BuildManager on application pre-init (&lt;em&gt;make sure you set the privatePath on the probing element in your web.config first!!&lt;/em&gt;)&lt;/p&gt;
&lt;div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:6bc6b791-155d-43bb-a79a-06f5660a311f" class="wlWriterSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"&gt;&lt;pre style="overflow: auto; height: 708px; width: 680px; background-color: white"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Linq;
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Web;
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.IO;
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Web.Hosting;
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Web.Compilation;
&lt;/span&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;&lt;span style="color: #000000"&gt; System.Reflection;

[assembly: PreApplicationStartMethod(&lt;/span&gt;&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;&lt;span style="color: #000000"&gt;(PluginFramework.Plugins.PreApplicationInit), &lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;Initialize&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;)]

&lt;/span&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt;&lt;span style="color: #000000"&gt; PluginFramework.Plugins
{
    &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;class&lt;/span&gt;&lt;span style="color: #000000"&gt; PreApplicationInit
    {

        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; PreApplicationInit()
        {
            PluginFolder &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DirectoryInfo(HostingEnvironment.MapPath(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;~/plugins&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;));
            ShadowCopyFolder &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;new&lt;/span&gt;&lt;span style="color: #000000"&gt; DirectoryInfo(HostingEnvironment.MapPath(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;~/plugins/temp&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;));
        }

        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; The source plugin folder from which to shadow copy from
        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;remarks&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; This folder can contain sub folderst to organize plugin types
        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/remarks&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000"&gt; DirectoryInfo PluginFolder;

        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000"&gt;
        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; The folder to shadow copy the plugin DLLs to use for running the app
        &lt;/span&gt;&lt;span style="color: #808080"&gt;///&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #808080"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000"&gt; DirectoryInfo ShadowCopyFolder;

        &lt;/span&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt;&lt;span style="color: #000000"&gt; &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt;&lt;span style="color: #000000"&gt; Initialize()
        {            
            Directory.CreateDirectory(ShadowCopyFolder.FullName);

            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;clear out plugins)&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (var f &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; ShadowCopyFolder.GetFiles(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;*.dll&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, SearchOption.AllDirectories))
            {
                f.Delete();
            }            

            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;shadow copy files&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (var plug &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt; PluginFolder.GetFiles(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;*.dll&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, SearchOption.AllDirectories))
            {
                var di &lt;/span&gt;&lt;span style="color: #000000"&gt;=&lt;/span&gt;&lt;span style="color: #000000"&gt; Directory.CreateDirectory(Path.Combine(ShadowCopyFolder.FullName, plug.Directory.Name));
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; NOTE: You cannot rename the plugin DLL to a different name, it will fail because the assembly name is part if it's manifest
                &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; (a reference to how assemblies are loaded: &lt;/span&gt;&lt;span style="color: #008000; text-decoration: underline"&gt;http://msdn.microsoft.com/en-us/library/yx7xezcf&lt;/span&gt;&lt;span style="color: #008000"&gt; )&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;                File.Copy(plug.FullName, Path.Combine(di.FullName, plug.Name), &lt;/span&gt;&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;&lt;span style="color: #000000"&gt;);
            }
            
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; Now, we need to tell the BuildManager that our plugin DLLs exists and to reference them.
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; There are different Assembly Load Contexts that we need to take into account which 
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; are defined in this article here:
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; &lt;/span&gt;&lt;span style="color: #008000; text-decoration: underline"&gt;http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx&lt;/span&gt;&lt;span style="color: #008000"&gt;

            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; * This will put the plugin assemblies in the 'Load' context
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; This works but requires a 'probing' folder be defined in the web.config&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000"&gt; (var a &lt;/span&gt;&lt;span style="color: #0000ff"&gt;in&lt;/span&gt;&lt;span style="color: #000000"&gt;
                ShadowCopyFolder
                .GetFiles(&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #800000"&gt;*.dll&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #000000"&gt;, SearchOption.AllDirectories)
                .Select(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt;  AssemblyName.GetAssemblyName(x.FullName))
                .Select(x &lt;/span&gt;&lt;span style="color: #000000"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000"&gt; Assembly.Load(x.FullName)))
            {
                BuildManager.AddReferencedAssembly(a);
            }

            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; * This will put the plugin assemblies in the 'LoadFrom' context
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; This works but requires a 'probing' folder be defined in the web.config
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; This is the slowest and most error prone version of the Load contexts.            
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;foreach (var a in
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    ShadowCopyFolder
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    .GetFiles("*.dll", SearchOption.AllDirectories)
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    .Select(plug =&amp;gt; Assembly.LoadFrom(plug.FullName)))
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;{
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    BuildManager.AddReferencedAssembly(a);
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;}

            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; * This will put the plugin assemblies in the 'Neither' context ( i think )
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; This nearly works but fails during view compilation.
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; This DOES work for resolving controllers but during view compilation which is done with the RazorViewEngine, 
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt; the CodeDom building doesn't reference the plugin assemblies directly.
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;foreach (var a in
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    ShadowCopyFolder
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    .GetFiles("*.dll", SearchOption.AllDirectories)
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    .Select(plug =&amp;gt; Assembly.Load(File.ReadAllBytes(plug.FullName))))
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;{
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;    BuildManager.AddReferencedAssembly(a);
            &lt;/span&gt;&lt;span style="color: #008000"&gt;//&lt;/span&gt;&lt;span style="color: #008000"&gt;}&lt;/span&gt;&lt;span style="color: #008000"&gt;
&lt;/span&gt;&lt;span style="color: #000000"&gt;
        }
    }
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1261</guid>
      <link>https://shazwazza.com/post/htmlhelper-table-methods/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>HtmlHelper Table methods</title>
      <description>&lt;p&gt;I was hoping that these were built already but couldn’t see any descent results with a quick Google search so figured I’d just write my own… these are now part of the &lt;a href="http://umbraco.com" target="_blank"&gt;Umbraco&lt;/a&gt; v5 codebase. Here’s 2 &lt;em&gt;HtmlHelper&lt;/em&gt; methods that allow you to very quickly create an Html table output based on an &lt;em&gt;IEnumerable&lt;/em&gt; data structure. Luckily for us, &lt;a href="http://twitter.com/haacked" target="_blank"&gt;Phil Haack&lt;/a&gt; showed us what &lt;a href="http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx" target="_blank"&gt;Templated Razor Delegates&lt;/a&gt; are which makes stuff like this very elegant. &lt;/p&gt;  &lt;p&gt;There are 2 types of tables: a table that expands vertically (you set a maximum number of columns), or a table that expands horizontally (you set a maximum amount of rows). So I’ve created 2 &lt;em&gt;HtmlHelper&lt;/em&gt; extensions for this:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:8dce357e-d507-4d16-91c0-92224da7c2f1" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 680px; height: 47px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;Html.TableHorizontal
Html.TableVertical
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Say you have a partial view with a declaration of something like:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:947e9b00-d408-46f9-8fa2-1d5472cf8988" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 680px; height: 34px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;@model IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;MyDataObject&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;To render a table is just one call and you can customize exactly how each cell is rendered and styled using Razor delegates, in the following example the MyDataObject class contains 3 properties: Name, Enabled and Icon and each cell is going to render a check box with a CSS image as it’s label and there will be a maximum of 8 columns:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:2c4c1d3d-1fc0-4762-8a9d-8c22d29aa8ae" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 680px; height: 127px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;@Html.TableVertical(Model, &lt;/span&gt;&lt;span style="color: #800080;"&gt;8&lt;/span&gt;&lt;span style="color: #000000;"&gt;, 
    @&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;text&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    @Html.CheckBox(@item.Name, @item.Enabled)
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;label &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;@Html.Id(@item.Name)&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;@item.Icon&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
        &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;span&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;@item.Name&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;span&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;label&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt;text&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;Nice! that’s it, far too easy. One thing to note is the &lt;em&gt;@&amp;lt;text&amp;gt; &amp;lt;/text&amp;gt;&lt;/em&gt; syntax as this is special Razor parser syntax that declares a Razor delegate without having to render additional html structures. Generally Razor delegates will require some Html tags such as this: &lt;em&gt;@&amp;lt;div&amp;gt; &amp;lt;/div&amp;gt; &lt;/em&gt;but if you don’t require surrounding Html structures, you can use the special &lt;em&gt;@&amp;lt;text&amp;gt; &amp;lt;/text&amp;gt;&lt;/em&gt; tags.&lt;/p&gt;

&lt;p&gt;Lastly here’s all the &lt;em&gt;HtmlHelper &lt;/em&gt;code… I’ve created these extensions as &lt;em&gt;HtmlHelper &lt;/em&gt;extensions only for consistency but as you’ll be able to see, the &lt;em&gt;HtmlHelper &lt;/em&gt;is never actually used and these extensions could simply be extensions of &lt;em&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/em&gt;.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dfc9f7fd-da9a-42a0-91ba-69ba26b35e1c" class="wlWriterEditableSmartContent"&gt;&lt;pre style=" width: 680px; height: 1000px;background-color:White;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Creates an Html table based on the collection 
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; which has a maximum numer of rows (expands horizontally)
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; HelperResult TableHorizontal&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt; HtmlHelper html,
    IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; collection,
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxRows,
    Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T, HelperResult&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; template) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; T : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; HelperResult(writer &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    {
        var items &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; collection.ToArray();
        var itemCount &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; items.Count();
        var maxCols &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Convert.ToInt32(Math.Ceiling(items.Count() &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; Convert.ToDecimal(maxRows)));
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;construct a grid first&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        var grid &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; T[maxCols, maxRows];
        var current &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; x &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxCols; x&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var y &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; y &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxRows; y&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (current &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; itemCount)
                    grid[x, y] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; items[current&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;];
        WriteTable(grid, writer, maxRows, maxCols, template);
    });
}

&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Creates an Html table based on the collection 
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; which has a maximum number of cols (expands vertically)
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; HelperResult TableVertical&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt; HtmlHelper html,
    IEnumerable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; collection,
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxCols,
    Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T, HelperResult&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; template) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; T : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;
{
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; HelperResult(writer &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;
    {
        var items &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; collection.ToArray();
        var itemCount &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; items.Count();
        var maxRows &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Convert.ToInt32(Math.Ceiling(items.Count() &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; Convert.ToDecimal(maxCols)));
        &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;construct a grid first&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;        var grid &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; T[maxCols, maxRows];
        var current &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var y &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; y &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxRows; y&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; x &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxCols; x&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;)                    
                &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (current &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; itemCount)
                    grid[x, y] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; items[current&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;];
        WriteTable(grid, writer, maxRows, maxCols, template);
    });
}

&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; Writes the table markup to the writer based on the item
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; input and the pre-determined grid of items
&lt;/span&gt;&lt;span style="color: #808080;"&gt;///&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;/span&gt;&lt;span style="color: #808080;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span style="color: #808080;"&gt;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; WriteTable&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(T[,] grid, 
    TextWriter writer, 
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxRows, 
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxCols, 
    Func&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;T, HelperResult&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; template) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt; T : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt;
{            
    &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;create a table based on the grid&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;    writer.Write(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;lt;table&amp;gt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var y &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; y &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxRows; y&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
    {
        writer.Write(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; x &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; maxCols; x&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
        {
            writer.Write(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
            var item &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; grid[x, y];
            &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (item &lt;/span&gt;&lt;span style="color: #000000;"&gt;!=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
            {
                &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;if there's an item at that grid location, call its template&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;                template(item).WriteTo(writer);
            }
            writer.Write(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        }
        writer.Write(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    }
    writer.Write(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1270</guid>
      <link>https://shazwazza.com/post/umbraco-jupiter-plugins-part-2-routing/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>Umbraco Jupiter Plugins - Part 2 - Routing</title>
      <description>&lt;p&gt;This is the second blog post in a series of posts relating to building plugins for Umbraco v5 (Jupiter). &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Related Posts:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-1.aspx"&gt;Umbraco Jupiter Plugins – Part 1&lt;/a&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;h2&gt;Disclaimer&lt;/h2&gt;  &lt;p&gt;&lt;em&gt;This post is about developing for Umbraco v5 (Jupiter) which at the time of this post is still under development. The technical details described below may change by the time Umbraco Jupiter is released. If you have feedback on the technical implementation details, please comment below.&lt;/em&gt;&lt;/p&gt;  &lt;h1&gt;Routing &amp;amp; URLs&lt;/h1&gt;  &lt;p&gt;As mentioned in the &lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-1.aspx"&gt;previous post&lt;/a&gt; Umbraco Jupiter will consist of many types of plugins, and of those plugins many of them exist as &lt;a href="http://www.asp.net/mvc/tutorials/asp-net-mvc-controller-overview-cs" target="_blank"&gt;MVC Controllers&lt;/a&gt;.&amp;#160; Each controller has an Action which a URL is routed to, this means that each Controller plugin in Jupiter requires it’s own unique URLs. The good news is that you as a package developer need not worry about managing these URLs and routes, Jupiter will conveniently do all of this for you. &lt;/p&gt;  &lt;h1&gt;Packages &amp;amp; Areas&lt;/h1&gt;  &lt;p&gt;My previous post mentioned that a ‘Package’ in Jupiter is a collection of ‘Plugins’ and as it turns out, Plugins can’t really function unless they are part of a Package. In it’s simplest form, a Package in v5 is a folder which contains Plugins that exists in the &lt;strong&gt;~/Plugins/Packages&lt;/strong&gt; sub folder. The folder name of the package becomes very important because it is used in setting up the routes to&amp;#160; create the unique URLs which map to the MVC Controller plugins. Package developers should be aware that they should name their folder to something that is reasonably unique so it doesn’t overlap with other Package folder names. During package installation, Jupiter will check for uniqueness in Package folder names to ensure there is no overlap (&lt;em&gt;there will be an entirely separate post on how to create deployment packages and how to install them&lt;/em&gt;).&lt;/p&gt;  &lt;p&gt;Here’s a quick example: If I have a Package that contains a &lt;em&gt;Tree &lt;/em&gt;plugin called &lt;em&gt;TestTree&lt;/em&gt; (which is in fact an MVC Controller) and I’ve called my Package folder ‘&lt;em&gt;Shazwazza’&lt;/em&gt;, which exists at &lt;strong&gt;~/Plugins/Packages/Shazwazza &lt;/strong&gt;then the URL to return the JSON for the tree is: &lt;a href="http://localhost/Umbraco/Shazwazza/Trees/TestTree/Index"&gt;http://localhost/Umbraco/Shazwazza/Trees/TestTree/Index&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Similarly, if I have a &lt;em&gt;Editor&lt;/em&gt; plugin called &lt;em&gt;TestEditor &lt;/em&gt;with an action called &lt;em&gt;Edit&lt;/em&gt;, then a URL to render the Edit Action is:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://localhost/Umbraco/Shazwazza/Editors/TestEditor/Edit"&gt;http://localhost/Umbraco/Shazwazza/Editors/TestEditor/Edit&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;If you’ve worked with MVC, you’ll probably know what an &lt;a href="http://msdn.microsoft.com/en-us/library/ee671793.aspx" target="_blank"&gt;MVC Area&lt;/a&gt; is. The way that Jupiter creates the routes for Packages/Plugins is by creating an MVC Area for each Package. This is how it deals with the probability that different Package developers may create MVC Controllers with the same name. MVC routes are generally based just on a Controller name and an Action name which wouldn’t really work for us because there’s bound to be overlap amongst Package developers, so by creating an Area for each Package the route becomes unique to a combination of Controller name, Action name and Area name.&amp;#160; MVC also determines where to look for Views based on Area name which solves another issue of multiple Packages installed with the same View names.&lt;/p&gt;  &lt;h1&gt;Whats next?&lt;/h1&gt;  &lt;p&gt;In the coming blog posts I’ll talk about &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;how plugins are installed and loaded &lt;/li&gt;    &lt;li&gt;how and where the Views are stored that the plugins reference &lt;/li&gt;    &lt;li&gt;how to create all of the different types of plugins &lt;/li&gt; &lt;/ul&gt;  &lt;h1&gt;Code Garden 2011&lt;/h1&gt;  &lt;p&gt;I’ll be doing a talk on &lt;a href="http://codegarden11.com/sessions/day-2/slot-one/get-plugged-in-to-umbraco-jupiter.aspx" target="_blank"&gt;Plugins for Umbraco Jupiter&lt;/a&gt; at &lt;a href="http://codegarden11.com/" target="_blank"&gt;Code Garden 2011&lt;/a&gt; this year which will go in to a lot more detail than these blog posts. If you are attending Code Garden already, then hopefully this series will give you a head start on Umbraco v5. If you haven’t got your tickets to Code Garden yet, what are you waiting for?! We have so much information to share with you :)&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:09 Z</pubDate>
      <a10:updated>2023-03-23T15:08:09Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1225</guid>
      <link>https://shazwazza.com/post/native-mvc-support-in-umbraco-coming-very-soon/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>Native MVC support in Umbraco coming very soon!</title>
      <description>&lt;p&gt;Its been a while since writing a blog post! … but that’s only because I can never find the time since I’m still gallivanting around the globe :P&lt;/p&gt; &lt;p&gt;But this post is about something very exciting, and I’m sure most of you that are reading this already know that with the upcoming Umbraco 4.10.0 release (currently in Beta and downloadable &lt;a href="http://umbraco.codeplex.com/releases/view/96853" target="_blank"&gt;here&lt;/a&gt;) we are natively supporting ASP.Net MVC! What’s more is that I’ve tried to document as much as I could on our GitHub docs. Once my pull request is completed the docs will all be available on the main site but until then you can see them on my fork &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/index.md" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;So where to begin? Well, I’m going to try to keep this really short and sweet because I’m hoping you can find most of the info that you’ll want in the documentation.&lt;/p&gt; &lt;h2&gt;What is supported?&lt;/h2&gt; &lt;p&gt;Everything that you can do in MVC you will be able to do in Umbraco’s MVC implementation. Anything from &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/partial-views.md" target="_blank"&gt;Partial Views&lt;/a&gt;, &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/child-actions.md" target="_blank"&gt;Child Actions&lt;/a&gt;, &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/forms.md" target="_blank"&gt;form submissions&lt;/a&gt;, data annotations, client validation to &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/surface-controllers.md" target="_blank"&gt;Surface Controllers&lt;/a&gt;, &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/custom-routes.md" target="_blank"&gt;custom routes&lt;/a&gt; and &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/custom-controllers.md" target="_blank"&gt;hijacking Umbraco routes&lt;/a&gt;.&amp;nbsp; If you have used Razor macros before, we support a very similar syntax &lt;strong&gt;but&lt;/strong&gt; it is not 100% the same. We support most of the dynamic querying that you use in Razor macros with the same syntax but to access the dynamic model is slightly different. What is crazy awesome super cool though is that we support a &lt;strong&gt;strongly typed&lt;/strong&gt; query structure!! :) So yes, you can do strongly typed Linq queries with &lt;strong&gt;intellisense&lt;/strong&gt;, no problemo! &lt;/p&gt; &lt;p&gt;Still using Web forms? not a problem, you can have both Web forms and MVC engines running in tandem on your Umbraco site. However, a config setting will set a default rendering engine which will determine whether Web forms master pages or MVC views are created in the back office.&lt;/p&gt; &lt;h2&gt;What is not supported (yet)&lt;/h2&gt; &lt;p&gt;There’s a few things in this release that we’ve had to push to 4.11.0 due to time constraints. They include: Better tooling support for the View editors in the back office, Partial View Macros and Child Action Macros. Though once you start using this MVC version you’ll quickly discover that the need for macros is pretty small. Perhaps people use macros in different ways but for the most part with the way that MVC works I would probably only use macros for rendering macro content in the WYSIWYG editor.&lt;/p&gt; &lt;p&gt;We support rendering any macros in MVC so you can even render your XSLT macros in your views, but issues will arise if you have User Control or Web form control macros that contain form elements. You will still be able to render these macros but any post backs will not work, and will most likely cause a YSOD. Unfortunately due to the vast differences between how Web forms and MVC work in ASP.Net this is something that we cannot support. The good news is that creating forms in MVC is super easy and makes a whole lot more sense than Web forms…. you can even have more than one &amp;lt;form&amp;gt; element on a page, who’d have thought :P &lt;/p&gt; &lt;h2&gt;Strongly typed queries&lt;/h2&gt; &lt;p&gt;You can find the documentation for this &lt;a href="https://github.com/Shandem/Umbraco4Docs/blob/4.8.0/Documentation/Reference/Mvc/querying.md" target="_blank"&gt;here&lt;/a&gt; but I just wanted to point out some of the differences between the strongly typed syntax and the dynamic syntax. First, in your view you have two properties:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;@Model.Content = the strongly typed model for your Umbraco view which is of type: &lt;em&gt;Umbraco.Core.Models.IPublishedContent&lt;/em&gt;  &lt;li&gt;@CurrentPage = the dynamic model representing the current page, this is very very similar to the @Model property in Razor macros&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;An example is to get the current page’s children that are visible, here’s the syntax for both (and of course since the @CurrentPage is dynamic, you will not get intellisense):&lt;/p&gt; &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #008000"&gt;//dynamic access&lt;/span&gt;&lt;br&gt;@CurrentPage.Children.Where(&lt;span style="color: #006080"&gt;"Visible"&lt;/span&gt;)&lt;br&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//strongly typed access&lt;/span&gt;&lt;br&gt;@Model.Content.Children.Where(x =&amp;gt; x.IsVisible())&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;There are also some queries that are just not (yet) possible in the dynamic query structure. In order to get some complex queries to work with dynamic linq, we need to code these in to the parser to create the expression tree. The parser could probably do with more TLC to support things like this but IMHO, we’re just better off using the strongly typed way instead (plus its probably a much faster execution). I’ve listed this as an example in the docs but will use it here again:&lt;/p&gt;
&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 365px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;&lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #008000"&gt;//This example gets the top level ancestor for the current node, and then gets &lt;/span&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//the first node found that contains "1173" in the array of comma delimited &lt;/span&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//values found in a property called 'selectedNodes'.&lt;/span&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//NOTE: This is one of the edge cases where this doesn't work with dynamic execution but the &lt;/span&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//syntax has been listed here to show you that its much easier to use the strongly typed query &lt;/span&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//instead&lt;/span&gt;&lt;br&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//dynamic access&lt;/span&gt;&lt;br&gt;var paramVals = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt; {{&lt;span style="color: #006080"&gt;"splitTerm"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;char&lt;/span&gt;[] {&lt;span style="color: #006080"&gt;','&lt;/span&gt;}}, {&lt;span style="color: #006080"&gt;"searchId"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"1173"&lt;/span&gt;}};&lt;br&gt;var result = @CurrentPage.Ancestors().OrderBy(&lt;span style="color: #006080"&gt;"level"&lt;/span&gt;)&lt;br&gt;    .Single()&lt;br&gt;    .Descendants()&lt;br&gt;    .Where(&lt;span style="color: #006080"&gt;"selectedNodes != null &amp;amp;&amp;amp; selectedNodes != String.Empty &amp;amp;&amp;amp; selectedNodes.Split(splitTerm).Contains(searchId)"&lt;/span&gt;, paramVals)&lt;br&gt;    .FirstOrDefault();&lt;br&gt;&lt;br&gt;&lt;span style="color: #008000"&gt;//strongly typed&lt;/span&gt;&lt;br&gt;var result = @Model.Content.Ancestors().OrderBy(x =&amp;gt; x.Level)&lt;br&gt;    .Single()&lt;br&gt;    .Descendants()&lt;br&gt;    .FirstOrDefault(x =&amp;gt; x.GetPropertyValue(&lt;span style="color: #006080"&gt;"selectedNodes"&lt;/span&gt;, &lt;span style="color: #006080"&gt;""&lt;/span&gt;).Split(&lt;span style="color: #006080"&gt;','&lt;/span&gt;).Contains(&lt;span style="color: #006080"&gt;"1173"&lt;/span&gt;));&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;

&lt;p&gt;IMHO i much prefer the strongly typed syntax but it’s up to you to decide since we support both structures.&lt;/p&gt;
&lt;h2&gt;UmbracoHelper&lt;/h2&gt;
&lt;p&gt;Another class we’ve introduced is called the &lt;em&gt;Umbraco.Web.UmbracoHelper &lt;/em&gt;which is accessible on your views by using the @Umbraco syntax and is also accessible on any SurfaceController. This class contains a ton of handy methods, it is basically the ‘new’ Umbraco ‘library’ class (you know that static one that has a lower case ‘l’ :P ) Of course the ‘library’ class will still be around and you can still use it in your views… &lt;strong&gt;but you shouldn’t! &lt;/strong&gt;This new helper class should contain all of the methods that you need from querying content/media by ids, rendering macros and rendering field content, to stripping the html from a string. The library class was designed for use in Xslt where everything from c# wasn’t natively given to you, now with Razor views you have the entire world of c# at your fingertips. So you’ll notice things like the date formatting functions that were on ‘library’ are not on the UmbracoHelper, and that is because you can just use the regular c# way. Though, if you feel inclined that these methods should be on UmbracoHelper, another great thing is that this is not a static class so you can add as many extension methods as you like.&amp;nbsp; I’d list all of the methods out here but I said I’d keep it short, your best bet currently is to just have a look at the class in the source code, or just see what great stuff shows up in intellisense in VS.&lt;/p&gt;
&lt;h2&gt;UmbracoContext&lt;/h2&gt;
&lt;p&gt;Though we did have another UmbracoContext class, this one is the new cooler one and the old one has been marked as obsolete. This new one’s full name is &lt;em&gt;Umbraco.Web.UmbracoContext &lt;/em&gt;and it is a singleton so you can access it by UmbracoContext.Current but normally you shouldn’t have to access it by it’s singleton because it is available in all of your views and SurfaceControllers as a property. For example, to access the UmbracoContext in your view you simply use &lt;em&gt;@UmbracoContext. &lt;/em&gt;This class exposes some handy properties like: &lt;em&gt;UmbracoUser, PageId, IsDebug,&lt;/em&gt; etc… &lt;/p&gt;
&lt;h2&gt;Testing&lt;/h2&gt;
&lt;p&gt;If you are interested in MVC and Umbraco, it would be really really appreciated for you to take the time to download the beta, the latest nightlies or source code and give it a shot. Hopefully the docs are enough to get you up and running and if you run into any troubles please log your issues on the &lt;a href="http://issues.umbraco.org" target="_blank"&gt;tracker&lt;/a&gt;. If you have any question, comments, etc… about the source code we’d love to hear from you on the &lt;a href="https://groups.google.com/forum/?pli=1#!forum/umbraco-dev" target="_blank"&gt;mail list&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Pete will be putting up an MVC getting started post on the Umbraco blog real soon, so &lt;a href="http://umbraco.com/follow-us.aspx" target="_blank"&gt;watch this space&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;Adios!&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1230</guid>
      <link>https://shazwazza.com/post/umbraco-v5-surface-controller-forms/</link>
      <category>Umbraco</category>
      <title>Umbraco v5 Surface Controller Forms</title>
      <description>&lt;p&gt;This post will show you how to create a form in Umbraco v5 using Surface Controllers. The information in this post assumes that you are familiar with Surface Controllers (see this &lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-5-Surface-Controllers.aspx" target="_blank"&gt;previous post&lt;/a&gt; if not) and with creating forms in ASP.Net MVC. &lt;/p&gt; &lt;h2&gt;Disclaimer&lt;/h2&gt; &lt;p&gt;&lt;em&gt;This post is about developing for Umbraco v5 (Jupiter) which at the time of this post is still under development. The technical details described below may change by the time Umbraco Jupiter is released. If you have feedback on the technical implementation details, please comment below.&lt;/em&gt;  &lt;h1&gt;Create a model&lt;/h1&gt; &lt;p&gt;The easiest way to create a form in MVC is to create a model that represents your form. This isn’t mandatory but then you’ll have to use some old school techniques like getting posted values directly from the HttpRequest object.&lt;/p&gt; &lt;p&gt;An example could be:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; MySite.Models
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyTestModel
    {
        [Required]
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Name { get; set; }

        [Required]
        [Range(18,30)]
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Age { get; set; }        
    }
}&lt;/pre&gt;
&lt;h1&gt;Create a Surface Controller&lt;/h1&gt;
&lt;p&gt;For this example we’ll assume that we’re creating a locally declared Surface Controller (not a plugin, &lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-5-Surface-Controllers.aspx" target="_blank"&gt;see the previous post for full details&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Some code first and explanation after:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; MySite.Controllers
{   
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MySurfaceController : SurfaceController
    {
        [HttpPost] 
        &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult HandleFormSubmit(
            [Bind(Prefix = &lt;span class="str"&gt;"MyTestForm"&lt;/span&gt;)]
            MyTestModel model)
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!ModelState.IsValid)
            {
                &lt;span class="kwrd"&gt;return&lt;/span&gt; CurrentUmbracoPage();
            }            
            
            &lt;span class="rem"&gt;//do stuff here with the data in the model... send&lt;/span&gt;
            &lt;span class="rem"&gt;// an email, or insert into db, etc...&lt;/span&gt;
            
            &lt;span class="kwrd"&gt;return&lt;/span&gt; RedirectToUmbracoPage(
                &lt;span class="kwrd"&gt;new&lt;/span&gt; HiveId(
                    &lt;span class="kwrd"&gt;new&lt;/span&gt; Guid(&lt;span class="str"&gt;"00000000000000000000000000001049"&lt;/span&gt;)));
        }
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Lets break down some parts of the above Controller code:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Namespace:&lt;/strong&gt; &lt;/em&gt;Generally you’ll put your locally declared controllers in the ~/Controllers folder, the above namespace reflects this.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Class:&lt;/strong&gt; &lt;/em&gt;The Surface Controller class is suffixed with the term ‘&lt;em&gt;SurfaceController’&lt;/em&gt;. This is a required convention (as per the previous post), without that suffix, the controller will not get routed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;[Bind] attribute: &lt;/em&gt;&lt;/strong&gt;In the code below you’ll see that we are creating the editor with a ‘prefix’ called ‘MyTestForm’. This ensures that each input element on the form will get its name prefixed with this value. For example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;input&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;="text"&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;="MyTestForm.Name"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;This is a recommended practice however it is optional. If you don’t prefix your form controls then you don’t need to use the [Bind] attribute. Here’s a few reasons why this is a recommended practice:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Without the prefix, If there is more than 1 form rendered on your webpage and both are using the MVC validation summary, then both validation summaries will display the errors for both forms. When there is a prefix you can tell the validation summary to only display errors for the form with the specified prefix. 
&lt;li&gt;The MVC Html helpers will generate the html id for each element and without specifying a prefix and if you have more than 1 form rendered on your page you may end up with duplicate html element Ids. 
&lt;li&gt;If you create a scaffolded form with a custom model object such as doing: &lt;br&gt;&lt;br&gt;&lt;pre class="csharpcode"&gt;@{ var someModel = &lt;span class="kwrd"&gt;new&lt;/span&gt; MyModel(); }
@Html.EditorFor(x =&amp;gt; someModel)&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;then MVC will automatically prefix your input fields with ‘someModel’ . For some reason MVC does this when the model name isn’t exactly ‘Model’.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;return CurrentUmbracoPage: &lt;/em&gt;&lt;/strong&gt;This method is built in to the base SurfaceController class and simply returns the currently executing Umbraco page without a redirect to it which is generally what you want to do in order to display any validation errors.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;return RedirectToUmbracoPage:&lt;/em&gt;&lt;/strong&gt; This method is built in to the base SurfaceController class and performs a redirect to a Umbraco page given an Id which is generally what you want to do when a form submission is successful. (chances are that you wont have a page with a Guid id of 00000000000000000000000000001234…. this is just an example :)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;NOTE: There is also a &lt;strong&gt;RedirectToCurrentUmbracoPage()&lt;/strong&gt; method!&lt;/em&gt;&lt;/p&gt;
&lt;h1&gt;Rendering a form&lt;/h1&gt;
&lt;p&gt;There are a few ways to render a form:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Directly in your Umbraco template/view 
&lt;li&gt;Using a &lt;a href="http://shazwazza.com/post/Partial-View-macros-in-Umbraco-v5.aspx" target="_blank"&gt;Partial View macro&lt;/a&gt; 
&lt;li&gt;Using a Child Action macro which renders a partial view&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Regardless of which you use to render a form, the markup will be very similar. Similar to MVC’s @Html.BeginForm, we have an &lt;strong&gt;@Html.BeginUmbracoForm&lt;/strong&gt; helper:&lt;/p&gt;&lt;pre class="csharpcode"&gt;@{
    var formModel = &lt;span class="kwrd"&gt;new&lt;/span&gt; MySite.Models.MyTestModel();
}

@&lt;span class="kwrd"&gt;using&lt;/span&gt;(Html.BeginUmbracoForm(&lt;span class="str"&gt;"HandleFormSubmit"&lt;/span&gt;, &lt;span class="str"&gt;"MySurface"&lt;/span&gt;))
{
    @Html.ValidationSummary(prefix: &lt;span class="str"&gt;"MyTestForm"&lt;/span&gt;)
    @Html.EditorFor(x =&amp;gt; formModel, &lt;span class="str"&gt;""&lt;/span&gt;, &lt;span class="str"&gt;"MyTestForm"&lt;/span&gt;)
    &amp;lt;input type=&lt;span class="str"&gt;"submit"&lt;/span&gt;/&amp;gt;
}&lt;/pre&gt;
&lt;p&gt;Here’s the breakdown of the above mark-up:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Html.BeginUmbracoForm: &lt;/em&gt;&lt;/strong&gt;A normal MVC form would simply use Html.BeginForm which will create an action attribute for the html form tag with a URL of your controller’s action. In Umbraco however, we want the URL to be posted to the same as the URL being rendered (post back), so the BeginUmbracoForm call handles this all for us. It will create a form tag with the URL of the currently rendered Umbraco node and add some custom hidden fields to your form containing the values of where the data will actually post to (your Surface Controller’s action). The Umbraco front-end route handler will take care of all of this for you.&lt;/p&gt;
&lt;p&gt;The parameters passed in to BeginUmbracoForm will differ depending on if your Surface Controller is a plugin controller or a locally declared controller. In this example, its a locally declared controller so we just need to give it the action name and controller name. If its a plugin Surface Controller, you’ll need to give it the action and and the controller ID. There’s also a few overloads so that you can add additional html attributes to the rendered html form tag.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;@Html.ValidationSummary: &lt;/em&gt;&lt;/strong&gt;The native MVC ValidationSummary doesn’t let you target specific input tags via prefixes so we’ve created a ‘better’ one that does let you do that. In this example we’re telling the validation summary to only display errors for input values that are prefixed with “MyTestForm” which is real handy if you’re rendering a few forms on the same page and using a validation summary for each.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;@Html.EditorFor: &lt;/em&gt;&lt;/strong&gt;This is the native EditorFor method in MVC which lets you supply a prefix name which will be used for all of your input fields. The MVC methods for rendering individual input tags also have an overload to supply a prefix if you choose not to have it scaffold your form for you.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;formModel: &lt;/em&gt;&lt;/strong&gt;The above mark-up will scaffold the form for us based on the model that we created previously. This example creates an inline model object (formModel) to scaffold the form but if you had a strongly typed partial view with your model type, you could just as well use the view’s Model property.&lt;/p&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/p&gt;
&lt;p&gt;And that’s pretty much it! Happy form making :)&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1235</guid>
      <link>https://shazwazza.com/post/multi-targeting-a-single-net-project-to-build-for-different-framework-versions/</link>
      <category>ASP.Net</category>
      <title>Multi targeting a single .Net project to build for different framework versions</title>
      <description>&lt;p&gt;Consider this scenario:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;I have a project that relies on ASP.Net MVC and currently this project is built against the .Net framework 4.0 and MVC 4&lt;/li&gt;
&lt;li&gt;I want to support MVC 5 for this project&lt;/li&gt;
&lt;li&gt;To support MVC 5, I’d need to upgrade the project to use .Net framework 4.5&lt;/li&gt;
&lt;li&gt;This would mean that developers still running .Net framework 4.0 would no longer be able to use my updated project&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also don’t want to have to create different projects and assemblies that target specific MVC versions &lt;em&gt;(in this case you’d end up with assembly names like MyProject.MVC4.dll and MyProject.MVC5.dll)&lt;/em&gt;. I have seen this done with other solutions and it works but I feel it is not necessary unless your project is using specific functionality from a particular MVC version.&lt;/p&gt;
&lt;p&gt;In my case my project will compile perfectly against MVC 4 and 5 without any codebase changes so I’m not actually targeting against a specific MVC version ( &amp;gt;= 4 ). If your project uses specific functionality from the different MVC versions I think you will have to output different assemblies like MyProject.MVC5.dll. In that case this post will probably help: &lt;a href="http://blogs.korzh.com/2013/12/nuget-package-different-mvc-versions.html" title="http://blogs.korzh.com/2013/12/nuget-package-different-mvc-versions.html"&gt;http://blogs.korzh.com/2013/12/nuget-package-different-mvc-versions.html&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;The goal&lt;/h2&gt;
&lt;p&gt;The end result is that I want a single project file that has 4 build configurations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Debug&lt;/strong&gt; / &lt;strong&gt;Release&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;These will build against .Net 4.0&lt;/li&gt;
&lt;li&gt;This will reference MVC 4&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Debug-Net45&lt;/strong&gt; / &lt;strong&gt;Release-Net45&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;This will build against .Net 4.5&lt;/li&gt;
&lt;li&gt;This will reference MVC 5&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Each of these build configurations exports the same assembly name: &lt;em&gt;MyProject.dll&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I then want a single NuGet package which will install the correct DLL based on the .Net version that the user has installed.&lt;/p&gt;
&lt;h2&gt;How it’s done&lt;/h2&gt;
&lt;h3&gt;Build configuration setup&lt;/h3&gt;
&lt;p&gt;To start with you’ll have a project that targets .Net framework 4.0 and references MVC 4 and 2 standard build configurations: &lt;em&gt;Debug, Release&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;First we need to create 2 new build configurations: &lt;em&gt;Debug-Net45 &lt;/em&gt;and &lt;em&gt;Release-Net45 &lt;/em&gt;and have these configurations output the DLLs to a custom folder. To do this we launch the Configuration Manager:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/2014-04-30_2014.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate-import/2014-04-30_2014_thumb.png" border="0" alt="2014-04-30_2014" title="2014-04-30_2014" width="244" height="119" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Create a new build configuration:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/2014-04-30_2016.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate-import/2014-04-30_2016_thumb.png" border="0" alt="2014-04-30_2016" title="2014-04-30_2016" width="244" height="103" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enter the name of the new configuration and copy the configuration from it’s associated existing one. So &lt;em&gt;Debug-Net45&lt;/em&gt; would copy from &lt;em&gt;Debug&lt;/em&gt; and &lt;em&gt;Release-Net45&lt;/em&gt; copies from &lt;em&gt;Release&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/2014-04-30_2021.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate-import/2014-04-30_2021_thumb.png" border="0" alt="2014-04-30_2021" title="2014-04-30_2021" width="244" height="130" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;ul&gt;&lt;!--EndFragment--&gt;&lt;/ul&gt;
&lt;/ul&gt;
&lt;p&gt;Do this for both configurations – &lt;em&gt;Debug-Net45&lt;/em&gt; and &lt;em&gt;Release-Net45&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;Project setup&lt;/h3&gt;
&lt;p&gt;The next step requires manually editing your .csproj file since Visual Studio doesn’t want to normally allow you to do this. This is how you can configure each build type to target a different framework version. You’ll first need to find these 2 entries:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PropertyGroup&lt;/span&gt; &lt;span class="attr"&gt;Condition&lt;/span&gt;&lt;span class="kwrd"&gt;=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PropertyGroup&lt;/span&gt; &lt;span class="attr"&gt;Condition&lt;/span&gt;&lt;span class="kwrd"&gt;=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Inside each of these elements you need to explicitly tell these build configurations to use .Net framework 4.0 by adding this element:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TargetFrameworkVersion&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;v4.0&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;TargetFrameworkVersion&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Next you’ll want to copy/paste both of these property groups and:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Change the &lt;em&gt;Condition&lt;/em&gt; to check for the new build configurations and to target .Net framework 4.5&lt;/li&gt;
&lt;li&gt;Change the &lt;em&gt;OutputPath&lt;/em&gt; to have a specific .Net 4.5 folder&lt;/li&gt;
&lt;li&gt;Change the &lt;span class="html"&gt;&lt;em&gt;TargetFrameworkVersion &lt;/em&gt;to be .Net 4.5&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You should end up with 2 new elements that look something like:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PropertyGroup&lt;/span&gt; &lt;span class="attr"&gt;Condition&lt;/span&gt;&lt;span class="kwrd"&gt;="'$(Configuration)|$(Platform)' == 'Debug-Net45|AnyCPU'"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DebugSymbols&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;true&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DebugSymbols&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;OutputPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;bin\Debug-Net45\&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;OutputPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DefineConstants&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;DEBUG;TRACE&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DefineConstants&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DebugType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;full&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DebugType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PlatformTarget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;AnyCPU&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;PlatformTarget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ErrorReport&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;prompt&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ErrorReport&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TargetFrameworkVersion&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;v4.5&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;TargetFrameworkVersion&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PlatformTarget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;AnyCPU&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;PlatformTarget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;PropertyGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PropertyGroup&lt;/span&gt; &lt;span class="attr"&gt;Condition&lt;/span&gt;&lt;span class="kwrd"&gt;="'$(Configuration)|$(Platform)' == 'Release-Net45|AnyCPU'"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DebugType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;pdbonly&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DebugType&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Optimize&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;true&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Optimize&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;OutputPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;bin\Release-Net45\&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;OutputPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;DefineConstants&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;TRACE&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;DefineConstants&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ErrorReport&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;prompt&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ErrorReport&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;WarningLevel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;4&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;WarningLevel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;TargetFrameworkVersion&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;v4.5&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;TargetFrameworkVersion&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;PlatformTarget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;AnyCPU&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;PlatformTarget&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;PropertyGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Next we need to do this for the MVC references or any framework specific references you need to target. In this example it is just the MVC references and you’ll need to wrap them with an &lt;em&gt;ItemGroup&lt;/em&gt; element, you’ll end up with something like this to target the MVC 4 libs:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt; &lt;span class="attr"&gt;Condition&lt;/span&gt;&lt;span class="kwrd"&gt;=" '$(Configuration)'=='Debug' Or '$(Configuration)'=='Release'"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="System.Web.Helpers, Version=2.0.0.0, ....."&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;True&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        ..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.Helpers.dll
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="System.Web.Mvc, Version=4.0.0.0, ....."&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;True&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        ..\packages\Microsoft.AspNet.Mvc.4.0.30506.0\lib\net40\System.Web.Mvc.dll
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="System.Web.Razor, Version=2.0.0.0, ....."&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;True&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        ..\packages\Microsoft.AspNet.Razor.2.0.30506.0\lib\net40\System.Web.Razor.dll
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="System.Web.WebPages, Version=2.0.0.0, ....."&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;True&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        ..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.dll
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="System.Web.WebPages.Deployment, Version=2.0.0.0, ....."&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;True&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        ..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Deployment.dll
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt; &lt;span class="attr"&gt;Include&lt;/span&gt;&lt;span class="kwrd"&gt;="System.Web.WebPages.Razor, Version=2.0.0.0, ....."&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;True&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Private&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        ..\packages\Microsoft.AspNet.WebPages.2.0.30506.0\lib\net40\System.Web.WebPages.Razor.dll
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;HintPath&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Reference&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ItemGroup&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Then we need to go ahead and copy/paste this block but target the different build configurations and then swap out these MVC 4 version references with the MVC 5 version references.&lt;/p&gt;
&lt;h3&gt;Nuget package restore&lt;/h3&gt;
&lt;p&gt;For this tutorial I’m assuming you are using Nuget to reference your MVC libs and an easy way to get Nuget to play reasonably with this setup is to enable Nuget package restore for your solution – right click on your solution and click the button:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/2014-05-01_1625.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate-import/2014-05-01_1625_thumb.png" border="0" alt="2014-05-01_1625" title="2014-05-01_1625" width="244" height="173" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Then in your packages.config file you can manually add the MVC 5 references:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0"&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;="utf-8"&lt;/span&gt;?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;packages&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;package&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.Mvc"&lt;/span&gt; 
           &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="4.0.30506.0"&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net40"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;package&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.Razor"&lt;/span&gt; 
           &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="2.0.30506.0"&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net40"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;package&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.WebPages"&lt;/span&gt; 
           &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="2.0.30506.0"&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net40"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;package&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.Web.Infrastructure"&lt;/span&gt; 
           &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="1.0.0.0"&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net40"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;package&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.Mvc"&lt;/span&gt; 
           &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="5.0.0"&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net45"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;package&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.Razor"&lt;/span&gt; 
           &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="3.0.0"&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net45"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;package&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.WebPages"&lt;/span&gt; 
           &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="3.0.0"&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net45"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;packages&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Now when you build your solution under the different build configurations Nuget will automatically go and get the correct MVC versions based on the current .Net Framework version.&lt;/p&gt;
&lt;h2&gt;Thats it!&lt;/h2&gt;
&lt;p&gt;With all of this in place it should ‘just work’ :) You will notice some odd looking icons in your references list in Visual Studio but it’s nothing to worry about, Visual Studio is just confused because it isn’t familiar with how you’ve tricked it!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/2014-05-01_1709.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate-import/2014-05-01_1709_thumb.png" border="0" alt="2014-05-01_1709" title="2014-05-01_1709" width="116" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So now when you build using your different build configurations, it will output the correct DLLs to the correct build folders:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Both &lt;em&gt;Debug &lt;/em&gt;&amp;amp; &lt;em&gt;Release &lt;/em&gt;will output to &lt;em&gt;/bin/Debug &lt;/em&gt;&amp;amp; &lt;em&gt;bin/Release &lt;/em&gt;with MVC 4 libraries and the MyProject.dll will be compiled against .Net Framework 4.0&lt;/li&gt;
&lt;li&gt;Both &lt;em&gt;Debug-Net45&lt;/em&gt; &amp;amp; &lt;em&gt;Release-Net45&lt;/em&gt; will be output to &lt;em&gt;/bin/Debug-Net45 &lt;/em&gt;&amp;amp; &lt;em&gt;/bin/Release-Net45 &lt;/em&gt;with MVC 5 libraries and the MyProject.dll will be compiled against .Net Framework 4.5&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And here’s some proof:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/2014-05-01_1714.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate-import/2014-05-01_1714_thumb.png" border="0" alt="2014-05-01_1714" title="2014-05-01_1714" width="244" height="181" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/2014-05-01_1715.png"&gt;&lt;img style="background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" src="https://shazwazza.com/media/articulate-import/2014-05-01_1715_thumb.png" border="0" alt="2014-05-01_1715" title="2014-05-01_1715" width="244" height="162" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you are using some automated build processes with MSBuild you can then just target the build configuration names you’d like to export.&lt;/p&gt;
&lt;p&gt;The Nuget setup is pretty simple since you can target your dependencies by framework:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;group&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net40"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;&amp;lt;!--between 4 and less than version 5--&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;dependency&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.Mvc"&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="(4.0.20710,5)"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;group&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;group&lt;/span&gt; &lt;span class="attr"&gt;targetFramework&lt;/span&gt;&lt;span class="kwrd"&gt;="net45"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;&amp;lt;!--between 4 and less than version 6 (who knows what'll happen then)--&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;dependency&lt;/span&gt; &lt;span class="attr"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;="Microsoft.AspNet.Mvc"&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;="(4.0.20710,6)"&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;group&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Then for each of your lib files in Nuget you ensure that you output them to the framework specific /lib folders such as:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;target="lib\net40"&lt;/pre&gt;
&lt;pre class="csharpcode"&gt;target="lib\net45"&lt;/pre&gt;
&lt;p&gt;This process has been implemented for the &lt;a href="https://github.com/Shandem/ClientDependency" target="_blank"&gt;Client Dependency Framework&lt;/a&gt; version &lt;a href="https://github.com/Shandem/ClientDependency/releases/tag/v1.8.0-beta" target="_blank"&gt;1.8.0&lt;/a&gt; to support MVC 4 and 5 without having to change the assembly name.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1256</guid>
      <link>https://shazwazza.com/post/sharing-views-between-specific-controllers/</link>
      <category>ASP.Net</category>
      <title>Sharing views between specific controllers</title>
      <description>&lt;p&gt;There’s no native way in MVC to share views between controllers without having to put those views in the ‘Shared’ folder which can be annoying especially if you have an inherited controller class structure, or your simply wish to just have one particular controller look for views in the folder of a different controller.&lt;/p&gt; &lt;p&gt;With a little help from &lt;a href="http://www.google.com.au/url?sa=t&amp;amp;source=web&amp;amp;cd=1&amp;amp;ved=0CB8QFjAA&amp;amp;url=http%3A%2F%2Fwww.jetbrains.com%2Fdecompiler%2F&amp;amp;ei=S28mTpqCEOKHmQWTrIzcCQ&amp;amp;usg=AFQjCNEHe70QThI6ntTvsyBhZHuW4K5F2A" target="_blank"&gt;DotPeek&lt;/a&gt; to see what’s going on inside of the &lt;em&gt;VirtualPathProviderViewEngine&lt;/em&gt;, I’ve created a fairly elegant solution to the above problem which consists of a custom ViewEngine and a custom controller Attribute. Lets start with the custom Attribute, its really simple and only contains one property which is the name of the controller that you wish the attributed controller to reference views.&amp;nbsp; An example:&lt;/p&gt;&lt;pre class="csharpcode"&gt;[AlternateViewEnginePath(&lt;span class="str"&gt;"ContentEditor"&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MediaEditorController : Controller
{  ...  }&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;The example above is saying that we have a MediaEditor controller that should reference views in the ContentEditor controller’s view folder, pretty simple right! A cool part about how the underlying ViewEngine works is that if a view isn’t found in the ContentEditor controller’s folder, then it will check back to the current controller’s folder, so it has built in fall back support.&lt;/p&gt;
&lt;p&gt;The custom ViewEngine is actually pretty simple as well once you know what’s going on inside of the &lt;em&gt;VirtualPathProviderViewEngine&lt;/em&gt;. There’s two particular methods: &lt;em&gt;FindView&lt;/em&gt; and &lt;em&gt;FindPartialView&lt;/em&gt; which the &lt;em&gt;VirtualPathProviderViewEngine&lt;/em&gt; figures out which controller/folder to look in. It figures this out by looking at the current ControllerContext’s RouteValues and simply does a lookup of the controller name by using this statement:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;controllerContext.RouteData.GetRequiredString("controller");&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So all we’ve got to do is override the &lt;em&gt;FindView&lt;/em&gt; and &lt;em&gt;FindPartialView&lt;/em&gt; methods, create a custom &lt;em&gt;ControllerContext&lt;/em&gt; with the “controller” route value to be the value specified in our custom attribute, and then pass this custom &lt;em&gt;ControllerContext&lt;/em&gt; into the underlying base &lt;em&gt;FindView&lt;/em&gt;/&lt;em&gt;FindPartial&lt;/em&gt; view methods:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// A ViewEngine that allows a controller's views to be shared with other &lt;/span&gt;
&lt;span class="rem"&gt;/// controllers without having to put these shared views in the 'Shared' folder.&lt;/span&gt;
&lt;span class="rem"&gt;/// This is useful for when you have inherited controllers.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AlternateLocationViewEngine : RazorViewEngine
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; ViewEngineResult FindPartialView(
        ControllerContext controllerContext, 
        &lt;span class="kwrd"&gt;string&lt;/span&gt; partialViewName, 
        &lt;span class="kwrd"&gt;bool&lt;/span&gt; useCache)
    {
        var altContext = GetAlternateControllerContext(controllerContext);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (altContext != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            &lt;span class="rem"&gt;//see if we can get the view with the alternate controller &lt;/span&gt;
            &lt;span class="rem"&gt;//specified, if its found return the result, if its not found&lt;/span&gt;
            &lt;span class="rem"&gt;//then return the normal results which will try to find &lt;/span&gt;
            &lt;span class="rem"&gt;//the view based on the 'real' controllers name.&lt;/span&gt;
            var result = &lt;span class="kwrd"&gt;base&lt;/span&gt;.FindPartialView(altContext, partialViewName,useCache);
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (result.View != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            {
                &lt;span class="kwrd"&gt;return&lt;/span&gt; result;
            }
        }
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;base&lt;/span&gt;.FindPartialView(controllerContext, partialViewName,useCache);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; ViewEngineResult FindView(
        ControllerContext controllerContext, 
        &lt;span class="kwrd"&gt;string&lt;/span&gt; viewName, 
        &lt;span class="kwrd"&gt;string&lt;/span&gt; masterName, 
        &lt;span class="kwrd"&gt;bool&lt;/span&gt; useCache)
    {
        var altContext = GetAlternateControllerContext(controllerContext);
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (altContext!= &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            var result = &lt;span class="kwrd"&gt;base&lt;/span&gt;.FindView(altContext, viewName, masterName, useCache);
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (result.View != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            {
                &lt;span class="kwrd"&gt;return&lt;/span&gt; result;
            }
        }
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;base&lt;/span&gt;.FindView(controllerContext, viewName, masterName, useCache);
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Returns a new controller context with the alternate controller name in the route values&lt;/span&gt;
    &lt;span class="rem"&gt;/// if the current controller is found to contain an AlternateViewEnginePathAttribute.&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;param name="currentContext"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; ControllerContext GetAlternateControllerContext(
        ControllerContext currentContext)
    {
        var controller = currentContext.Controller;
        var altControllerAttribute = controller.GetType()
            .GetCustomAttributes(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(AlternateViewEnginePathAttribute), &lt;span class="kwrd"&gt;false&lt;/span&gt;)
            .OfType&amp;lt;AlternateViewEnginePathAttribute&amp;gt;()
            .ToList();
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (altControllerAttribute.Any())
        {
            var altController = altControllerAttribute.Single().AlternateControllerName;
            &lt;span class="rem"&gt;//we're basically cloning the original route data here...&lt;/span&gt;
            var newRouteData = &lt;span class="kwrd"&gt;new&lt;/span&gt; RouteData
                {
                    Route = currentContext.RouteData.Route,
                    RouteHandler = currentContext.RouteData.RouteHandler
                };
            currentContext.RouteData.DataTokens
                .ForEach(x =&amp;gt; newRouteData.DataTokens.Add(x.Key, x.Value));
            currentContext.RouteData.Values
                .ForEach(x =&amp;gt; newRouteData.Values.Add(x.Key, x.Value));

            &lt;span class="rem"&gt;//now, update the new route data with the new alternate controller name&lt;/span&gt;
            newRouteData.Values[&lt;span class="str"&gt;"controller"&lt;/span&gt;] = altController;

            &lt;span class="rem"&gt;//now create a new controller context to pass to the view engine&lt;/span&gt;
            var newContext = &lt;span class="kwrd"&gt;new&lt;/span&gt; ControllerContext(
                currentContext.HttpContext, 
                newRouteData, 
                currentContext.Controller);
            &lt;span class="kwrd"&gt;return&lt;/span&gt; newContext;
        }

        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    }
}

&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// An attribute for a controller that specifies that the ViewEngine &lt;/span&gt;
&lt;span class="rem"&gt;/// should look for views for this controller using a different controllers name.&lt;/span&gt;
&lt;span class="rem"&gt;/// This is useful if you want to share views between specific controllers &lt;/span&gt;
&lt;span class="rem"&gt;/// but don't want to have to put all of the views into the Shared folder.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AlternateViewEnginePathAttribute : Attribute
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; AlternateControllerName { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; AlternateViewEnginePathAttribute(&lt;span class="kwrd"&gt;string&lt;/span&gt; altControllerName)
    {
        AlternateControllerName = altControllerName;
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Lastly, you’ll need to just register this additional ViewEngine in your global.asax, or IoC, or however you are doing that sort of thing in your application.</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1199</guid>
      <link>https://shazwazza.com/post/mvc-in-umbraco-v4/</link>
      <category>Umbraco</category>
      <title>MVC in Umbraco v4</title>
      <description>&lt;p&gt;After the &lt;a href="http://umbraco.com/follow-us/blog-archive/2012/6/13/v5-rip.aspx" target="_blank"&gt;announcement of discontinuing Umbraco v5 development&lt;/a&gt; during this years &lt;a href="http://codegarden12.com" target="_blank"&gt;Code Garden 2012&lt;/a&gt; conference, the biggest question seemed to be: &lt;strong&gt;What will happen with ASP.Net MVC and Umbraco&lt;/strong&gt;. We held an open space session regarding this question where we asked 2 questions: “Is an MVC implementation in Umbraco important to have for the front end?” and “Is an MVC implementation important to have for the back office?”. There were quite a few people who attended this session and I think 100% of people raised their hands in answer to the first question and only 1 person raised their hands for the second answer.&amp;nbsp; I think this result was expected since most Umbraco developers simply wish to use MVC in their own development on the front-end and want the Umbraco back office to keep working with all of the great packages already created for it.&lt;/p&gt; &lt;p&gt;There was a considerable amount of knowledge gained in creating v5 and of course we will incorporate much of this knowledge into v4… starting real soon! I decided to create a package for Umbraco v4 which enables a native MVC front end which was demo’d during the Code Garden package competition and is available right now!&amp;nbsp; It turns out that creating this didn’t take much time at all, in fact I created it during the 2nd day of the conference. The reason this was reasonable easy was because we had all of this already built for v5 so it was a matter of taking this knowledge and code and applying it to v4.&lt;/p&gt; &lt;p&gt;So if you are like many other devs and really want Umbraco to route to a native MVC front end then you can give this package a try, its called &lt;strong&gt;umbraMVCo&lt;/strong&gt;. Please be warned that I have not tested this very much, but also know that its just using the tried and tested v4 api’s to do the routing so I’m assuming it will ‘just work’ … though I’m sure you guys will find some bugs I haven’t yet :) There’s also a readme file on the downloads page which explains how to install it, and what settings/servers that I know it works with. &lt;/p&gt; &lt;p&gt;What’s also really cool is this code completely supports &lt;a href="http://shazwazza.com/post/Hijacking-Umbraco-routes.aspx" target="_blank"&gt;Hijacking Umbraco Routes&lt;/a&gt;!&lt;/p&gt; &lt;h2&gt;Download/Source&lt;/h2&gt; &lt;p&gt;You can download the package and readme files here &lt;/p&gt; &lt;p&gt;&lt;a href="https://bitbucket.org/Shandem/umbramvco/downloads"&gt;https://bitbucket.org/Shandem/umbramvco/downloads&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Ensure that you read the readme file, it contains some very important information including installation instructions!&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The source can be found here:&lt;/p&gt; &lt;p&gt;&lt;a href="https://bitbucket.org/Shandem/umbramvco/src"&gt;https://bitbucket.org/Shandem/umbramvco/src&lt;/a&gt;&lt;/p&gt; &lt;p&gt;If you come across issues with this please log issues here: &lt;a title="https://bitbucket.org/Shandem/umbramvco/issues" href="https://bitbucket.org/Shandem/umbramvco/issues"&gt;https://bitbucket.org/Shandem/umbramvco/issues&lt;/a&gt; . The source isn’t really complicated or anything though so you should be able to identify the reason for an issue if you come across it.&lt;/p&gt; &lt;h2&gt;What’s next?&lt;/h2&gt; &lt;p&gt;&lt;strong&gt;So will MVC be coming to v4… Yes indeed&lt;/strong&gt;. We will also support routing to either WebForms or MVC and we’ll support this by having a flag on the document type to choose which rendering engine to use. We’ve got a few things to do in v4 regarding how routing works to do this nicely/properly but the end result will be pretty much the same as what this package enables. All of those cool MVC things that you liked in v5 like SurfaceControllers will be coming to v4 as well, we will still support Child Action and Partial View macros. Another cool thing is that we should be able to support Xslt and UserControl macros in MVC as well (though any post backs will definitely not work). &lt;/p&gt; &lt;p&gt;So even though the development of v5 has been discontinued, a ton of the concepts that you liked about it will in fact be ported to v4. &lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1185</guid>
      <link>https://shazwazza.com/post/umbraco-jupiter-plugins-part-4-editors/</link>
      <category>Umbraco</category>
      <title>Umbraco Jupiter Plugins - Part 4 - Editors</title>
      <description>&lt;p&gt;This is the fourth blog post in a series of posts relating to building plugins for Umbraco v5 (Jupiter). This post will show you how to &lt;strong&gt;get started &lt;/strong&gt;with building an editor. An Editor is the term used to express the editing pane on the right hand side of the back-office. Examples include: the content editor, media editor, document type editor, script editor, etc..&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Related Posts:&lt;/strong&gt;  &lt;ol&gt; &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-1.aspx"&gt;Umbraco Jupiter Plugins – Part 1&lt;/a&gt;  &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-2-Routing.aspx"&gt;Umbraco Jupiter Plugins – Part 2 – Routing&lt;/a&gt;  &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-3-Trees.aspx" target="_blank"&gt;Umbraco Jupiter Pluings – Part 3 – Trees&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h2&gt;Disclaimer&lt;/h2&gt; &lt;p&gt;&lt;em&gt;This post is about developing for Umbraco v5 (Jupiter) which at the time of this post is still under development. The technical details described below may change by the time Umbraco Jupiter is released. If you have feedback on the technical implementation details, please comment below.&lt;/em&gt;  &lt;h1&gt;Defining an Editor&lt;/h1&gt; &lt;p&gt;An Editor in Umbraco v5 is the combination of: An MVC Controller, View(s), JavaScript and CSS. The first step to creating an Editor is to create a class that inherits from the Umbraco base editor class: &lt;strong&gt;Umbraco.Cms.Web.Editors.StandardEditorController.&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;The next step is to register this editor as an editor plugin. To do this you just need to add an attribute to your class such as:&lt;/p&gt;&lt;pre class="csharpcode"&gt;[Editor(&lt;span class="str"&gt;"ADD307B3-A5F9-4A89-ADAC-72289A5943FF"&lt;/span&gt;)]&lt;/pre&gt;
&lt;p&gt;The mandatory parameter passed to this attribute is the editor plugin ID (this MUST be unique so ensure you generated a new GUID for every single one of your plugins). 
&lt;p&gt;The next thing you’ll need to do to ensure your editor plugin is found and loaded is to ‘advertise’ that your assembly contains a plugin. To do this, just edit your assembly’s AssemblyInfo.cs file and add the following attribute:&lt;pre class="csharpcode"&gt;[assembly: AssemblyContainsPlugins]&lt;/pre&gt;
&lt;h1&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Creating an Editor&lt;/h1&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Important convention: &lt;/strong&gt;All Editor controller names MUST be suffixed with ‘EditorController’. For example, if you are creating an editor to display system information, you might call your Editor: SystemInfo&lt;strong&gt;EditorController&lt;/strong&gt;. If you don’t follow this convention, you’re editor controller wont be routed.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When creating an Editor there are a few base classes to choose from. Generally however, you should inherit from: &lt;strong&gt;Umbraco.Cms.Web.Editors.StandardEditorController&lt;/strong&gt;. The other base classes and their hierarchy are as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Umbraco.Cms.Web.Mvc.Controllers.BackOffice.SecuredBackOfficeController 
&lt;ul&gt;
&lt;li&gt;Umbraco.Cms.Web.Editors.BaseEditorController 
&lt;ul&gt;
&lt;li&gt;Umbraco.Cms.Web.Editors.DashboardEditorController 
&lt;ul&gt;
&lt;li&gt;Umbraco.Cms.Web.Editors.StandardEditorController&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;When inheriting from any of the base classes above, you will be required to create a constructor accepting an parameter of type: &lt;strong&gt;IBackOfficeRequestContext &lt;/strong&gt;:&lt;/p&gt;&lt;pre class="csharpcode"&gt;[Editor(&lt;span class="str"&gt;"ADD307B3-A5F9-4A89-ADAC-72289A5943FF"&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyEditorController : StandardEditorController
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyEditorController(IBackOfficeRequestContext requestContext)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(requestContext)
    {
    }        
}&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
The &lt;em&gt;StandardEditorController&lt;/em&gt; has an abstract method: &lt;em&gt;Edit(HiveId id)&lt;/em&gt; that needs to be implemented &lt;em&gt;(the abstract Edit Action is marked as [HttpGet])&lt;/em&gt;&lt;/p&gt;&lt;pre class="csharpcode"&gt;[Editor(&lt;span class="str"&gt;"ADD307B3-A5F9-4A89-ADAC-72289A5943FF"&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyEditorController : StandardEditorController
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyEditorController(IBackOfficeRequestContext requestContext)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(requestContext)
    {
    }
        
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; ActionResult Edit(HiveId id)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; View();
    }
}&lt;/pre&gt;
&lt;p&gt;Most Editors will be displaying a view to edit data based on a &lt;em&gt;HiveId &lt;/em&gt;which is the unique identifier type for pretty much all data in Umbraco 5. If you are writing an editor to edit data in a custom Hive provider, then this will work seamlessly for you. Even if you are creating an editor for data that you aren’t writing a custom Hive provider for, you can still use HiveId as a unique identifier since it has support for wrapping Guid, Int and String Id types. If however you decide that HiveId isn’t for you, then you can inherit from one of the other editor base classes that doesn’t have the abstract Edit method attached to it and create your own Actions with your own parameters.&lt;/p&gt;
&lt;p&gt;The above Edit Action simply returns a view without a model to be rendered. At this point, you’ll need to know where your view should be stored which has everything to do with MVC Areas or embedding views.&lt;/p&gt;
&lt;h1&gt;MVC Areas &amp;amp; Jupiter Packages&lt;/h1&gt;
&lt;p&gt;In a &lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-2-Routing.aspx" target="_blank"&gt;previous post&lt;/a&gt; we talk about how packages in v5 are actually registered as their own &lt;a href="http://msdn.microsoft.com/en-us/library/ee671793.aspx" target="_blank"&gt;MVC Area&lt;/a&gt;. All packages get installed to the following location: ~/App_Plugins/Packages/{YourPackageName} . If you aren’t embedding your views, then they should be stored inside of your package folder. Each plugin type has a specific view folder name that your views should be stored in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Editor views &amp;amp; partial views: 
&lt;ul&gt;
&lt;li&gt;~/App_Plugins/Packages/{&lt;em&gt;YourPackageName&lt;/em&gt;}/&lt;strong&gt;Editors&lt;/strong&gt;/Views/ {&lt;em&gt;EditorControllerName&lt;/em&gt;}/{ViewName}.cshtml 
&lt;li&gt;~/App_Plugins/Packages/{&lt;em&gt;YourPackageName&lt;/em&gt;}/&lt;strong&gt;Editors&lt;/strong&gt;/Views/Shared/ {&lt;em&gt;ViewName&lt;/em&gt;}.cshtml&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Property Editor partial views: 
&lt;ul&gt;
&lt;li&gt;~/App_Plugins/Packages/{&lt;em&gt;YourPackageName&lt;/em&gt;}/&lt;strong&gt;PropertyEditors&lt;/strong&gt;/Views/Shared/ {&lt;em&gt;ViewName&lt;/em&gt;}.cshtml&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Dashboard partial views: 
&lt;ul&gt;
&lt;li&gt;~/App_Plugins/Packages/{&lt;em&gt;YourPackageName&lt;/em&gt;}/&lt;strong&gt;Dashboards&lt;/strong&gt;/Views/ {&lt;em&gt;ViewName&lt;/em&gt;}.cshtml&lt;/li&gt;&lt;/ul&gt;
&lt;li&gt;Rendering (front-end) partial views: 
&lt;ul&gt;
&lt;li&gt;~/App_Plugins/Packages/{&lt;em&gt;YourPackageName&lt;/em&gt;}/&lt;strong&gt;Views&lt;/strong&gt;/Partial/ {&lt;em&gt;ViewName&lt;/em&gt;}.cshtml&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;So with the controller created above, I would have a view in the following location:&lt;/p&gt;
&lt;p&gt;~/App_Plugins/{MyPackageName}/Editors/Views/MyEditor/Edit.cshtml&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: The package name folder will be created when installing your NuGet package and will be based on your NuGet package name and version assigned.&lt;/em&gt;&lt;/p&gt;
&lt;h1&gt;Embedding views&lt;/h1&gt;
&lt;p&gt;Many of the views shipped with v5 are embedded which helps to reduce the number of actual files that are shipped. This is also handy if you don’t want to give the ability for people to change what’s in your markup.&lt;/p&gt;
&lt;p&gt;Embedding a view is really easy:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a Razor (.cshtml) view in your Package’s project 
&lt;li&gt;View the Properties for this file and choose &lt;em&gt;‘Embedded Resource’&lt;/em&gt; as the ‘Build Action’&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Now to use the embedded view we use the following syntax:&lt;/p&gt;&lt;pre class="csharpcode"&gt;[Editor(&lt;span class="str"&gt;"ADD307B3-A5F9-4A89-ADAC-72289A5943FF"&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyEditorController : StandardEditorController
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyEditorController(IBackOfficeRequestContext requestContext)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(requestContext)
    {
    }
        
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; ActionResult Edit(HiveId id)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; View(EmbeddedViewPath.Create(&lt;span class="str"&gt;"MyProject.Editors.Views.Edit.cshtml"&lt;/span&gt;));
    }
}&lt;/pre&gt;
&lt;p&gt;Its important to get the correct path to your view file. In this instance, my view’s path in my project is: &lt;em&gt;MyProject.Editors.Views.Edit.cshtml&lt;/em&gt;&lt;/p&gt;
&lt;h1&gt;Displaying your Editor&lt;/h1&gt;
&lt;p&gt;Most of the time an editor is displayed by clicking on a node in the tree or by accessing a context menu item. In a &lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-3-Trees.aspx" target="_blank"&gt;previous post&lt;/a&gt; about creating trees there was a method to create a tree node:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; UmbracoTreeResult GetTreeData(HiveEntityUri id, FormCollection queryStrings)
{
    NodeCollection.Add(
        CreateTreeNode(id, &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="str"&gt;"My only node"&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty, &lt;span class="kwrd"&gt;false&lt;/span&gt;));
    &lt;span class="kwrd"&gt;return&lt;/span&gt; UmbracoTree();
}&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
This simply created a tree node that had no editor URL but now that we have an editor, I’ll update the code to click through to my Edit Action:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; UmbracoTreeResult GetTreeData(HiveId id, FormCollection queryStrings)
{
    NodeCollection.Add(
        CreateTreeNode(id, &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="str"&gt;"My only node"&lt;/span&gt;,
        Url.GetEditorUrl(&lt;span class="str"&gt;"MyEditor"&lt;/span&gt;, id, &lt;span class="kwrd"&gt;new&lt;/span&gt; Guid(&lt;span class="str"&gt;"ADD307B3-A5F9-4A89-ADAC-72289A5943FF"&lt;/span&gt;)),
        &lt;span class="kwrd"&gt;false&lt;/span&gt;));
    &lt;span class="kwrd"&gt;return&lt;/span&gt; UmbracoTree();
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
GetEditorUrl is an extension method of UrlHelper which has a few overloads for generating an Editor’s Url. In this case we are passing in the Editor’s name and Id with the Id of the current node being rendered in the tree. 
&lt;p&gt;When the node is clicked it will now link to the Edit Action of the MyEditor Controller.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1151</guid>
      <link>https://shazwazza.com/post/custom-mvc-routing-in-umbraco/</link>
      <category>Umbraco</category>
      <title>Custom MVC routing in Umbraco</title>
      <description>&lt;p&gt;This post will describe how you can declare your own custom MVC routes in order to execute your own custom controllers in Umbraco but still be able to render Umbraco views with the same model that Umbraco uses natively. &lt;/p&gt; &lt;p&gt;&lt;em&gt;NOTE: This post is not about trying to execute a particular Umbraco page under a custom URL, that functionality can be accomplished by creating a custom IContentFinder (in v6.1), or by applying the &lt;/em&gt;&lt;a href="http://our.umbraco.org/wiki/reference/umbraco-best-practices/umbracourlalias" target="_blank"&gt;&lt;em&gt;umbracoUrlAlias&lt;/em&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;There’s a &lt;a href="http://our.umbraco.org/forum/developers/extending-umbraco/41367-Umbraco-6-MVC-Custom-MVC-Route" target="_blank"&gt;long (but very useful) thread on Our&lt;/a&gt; describing various needs for custom MVC routing inside of Umbraco, definitely worth a read. Here I’ll try to describe a pretty easy way to accomplish this. I’m using Umbraco v6.0.7 (but I’m pretty sure this will work in v4.10+ as well). &lt;/p&gt; &lt;h2&gt;Create the route&lt;/h2&gt; &lt;p&gt;This example will use an &lt;em&gt;IApplicationEventHandler&lt;/em&gt; (in 6.1 you should use the base class &lt;em&gt;ApplicationEventHandler&lt;/em&gt; instead). Here I’m defining a custom route for handling products on my website. The example URLs that I want handled will be:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;/Products/Product/ProductA&lt;/li&gt; &lt;li&gt;/Products/Category/CategoryA&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyStartupHandler : IApplicationEventHandler
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnApplicationStarted(
        UmbracoApplicationBase umbracoApplication, 
        ApplicationContext applicationContext)
    {
        &lt;span class="rem"&gt;//Create a custom route&lt;/span&gt;
        RouteTable.Routes.MapRoute(
            &lt;span class="str"&gt;"test"&lt;/span&gt;,
            &lt;span class="str"&gt;"Products/{action}/{id}"&lt;/span&gt;,
            &lt;span class="kwrd"&gt;new&lt;/span&gt;
                {
                    controller = &lt;span class="str"&gt;"MyProduct"&lt;/span&gt;, 
                    action = &lt;span class="str"&gt;"Product"&lt;/span&gt;, 
                    id = UrlParameter.Optional
                });           
    }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnApplicationInitialized(
        UmbracoApplicationBase umbracoApplication, 
        ApplicationContext applicationContext)
    {
    }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnApplicationStarting(
        UmbracoApplicationBase umbracoApplication, 
        ApplicationContext applicationContext)
    {
    }
}&lt;/pre&gt;
&lt;h2&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Create the controller&lt;/h2&gt;
&lt;p&gt;With the above route in place, I need to create a controller called “&lt;em&gt;MyProductController&lt;/em&gt;”. The base class it will inherit from will be “&lt;em&gt;Umbraco.Mvc.PluginController&lt;/em&gt;”. This abstract class exposes many of the underlying Umbraco objects that I might need to work with such as an &lt;em&gt;UmbracoHelper, UmbracoContext, ApplicationContext&lt;/em&gt;, etc… Also note that the &lt;em&gt;PluginController&lt;/em&gt; doesn’t get auto-routed like a &lt;em&gt;SurfaceController&lt;/em&gt; which is good because we only want to route our controller once. In 6.1 you can inherit from a different controller called &lt;em&gt;Umbraco.Mvc.UmbracoController&lt;/em&gt;, which is what the &lt;em&gt;PluginController&lt;/em&gt; will be inheriting from in the next version.&lt;/p&gt;
&lt;h3&gt;Constructor&lt;/h3&gt;
&lt;p&gt;First thing is to define the constructors since the &lt;em&gt;PluginController&lt;/em&gt; doesn’t have an empty constructor but we want ours to (unless you have IoC setup).&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyProductController : PluginController
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyProductController()
        : &lt;span class="kwrd"&gt;this&lt;/span&gt;(UmbracoContext.Current)
    {            
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; MyProductController(UmbracoContext umbracoContext) 
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(umbracoContext)
    {
    }
}&lt;/pre&gt;
&lt;h3&gt;Actions&lt;/h3&gt;
&lt;p&gt;Next we need to create the controller Actions. These actions will need to lookup either a Product or a Category based on the ‘id’ string they get passed. For example, given the following URL: /Products/Category/CategoryA the id would be &lt;strong&gt;CategoryA &lt;/strong&gt;and it would execute on the &lt;strong&gt;Category&lt;/strong&gt; action.&lt;/p&gt;
&lt;p&gt;In my Umbraco installation, I have 2 document types with aliases: “Product” and “ProductCategory”&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_35.png"&gt;&lt;img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_33.png" width="300" height="96"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To perform the lookup in the controller Actions we’ll use the UmbracoHelper.TypedSearch overload which uses Examine.&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Category(&lt;span class="kwrd"&gt;string&lt;/span&gt; id)
{
    var criteria = ExamineManager.Instance.DefaultSearchProvider
        .CreateSearchCriteria(&lt;span class="str"&gt;"content"&lt;/span&gt;);
    var filter = criteria.NodeTypeAlias(&lt;span class="str"&gt;"ProductCategory"&lt;/span&gt;).And().NodeName(id);
    var result = Umbraco.TypedSearch(filter.Compile()).ToArray();
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (!result.Any())
    {
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpException(404, &lt;span class="str"&gt;"No category"&lt;/span&gt;);
    }
    &lt;span class="kwrd"&gt;return&lt;/span&gt; View(&lt;span class="str"&gt;"ProductCategory"&lt;/span&gt;, CreateRenderModel(result.First()));
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Product(&lt;span class="kwrd"&gt;string&lt;/span&gt; id)
{
    var criteria = ExamineManager.Instance.DefaultSearchProvider
        .CreateSearchCriteria(&lt;span class="str"&gt;"content"&lt;/span&gt;);
    var filter = criteria.NodeTypeAlias(&lt;span class="str"&gt;"Product"&lt;/span&gt;).And().NodeName(id);
    var result = Umbraco.TypedSearch(filter.Compile()).ToArray();
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (!result.Any())
    {
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpException(404, &lt;span class="str"&gt;"No product"&lt;/span&gt;);
    }
    &lt;span class="kwrd"&gt;return&lt;/span&gt; View(&lt;span class="str"&gt;"Product"&lt;/span&gt;, CreateRenderModel(result.First()));
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;The Category action lookup uses Examine to lookup any document with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A document type alias of “ProductCategory” &lt;/li&gt;
&lt;li&gt;A name equal to the id parameter passed in&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The Product action lookup uses Examine to lookup any document with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A document type alias of “Product”&lt;/li&gt;
&lt;li&gt;A name equal to the id parameter passed in&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;The result from &lt;em&gt;TypedSearch&lt;/em&gt; is &lt;em&gt;IEnumerable&amp;lt;IPublishedContent&amp;gt;&lt;/em&gt; and since we know we only want one result we pass in the first item of the collection in “result.First()”&lt;/p&gt;
&lt;p&gt;If you didn’t want to use Examine to do the lookup, you could use a Linq query based on the result of &lt;em&gt;Umbraco.TypedContentAtRoot()&lt;/em&gt;, but I wouldn’t recommend that since it will be much slower.&lt;/p&gt;
&lt;p&gt;In v6.1 the UmbracoHelper exposes a couple of other methods that you could use to perform your lookup if you didn’t want to use Examine and wanted to use XPath instead:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;TypedContentSingleAtXPath(string xpath, params XPathVariable[] vars)&lt;/li&gt;
&lt;li&gt;TypedContentAtXPath(string xpath, params XPathVariable[] vars)&lt;/li&gt;
&lt;li&gt;TypedContentAtXPath(XPathExpression xpath, params XPathVariable[] vars)&lt;/li&gt;&lt;/ul&gt;
&lt;h3&gt;CreateRenderModel method&lt;/h3&gt;
&lt;p&gt;You will have noticed that I’m using a method called &lt;em&gt;CreateRenderModel&lt;/em&gt; to create the model that is passed to the View. This method accepts an &lt;em&gt;IPublishedContent&lt;/em&gt; object as an argument and creates a &lt;em&gt;RenderModel&lt;/em&gt; object which is what a normal Umbraco view expects. This method isn’t complex but it does have a couple things worth noting:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; RenderModel CreateRenderModel(IPublishedContent content)
{
    var model = &lt;span class="kwrd"&gt;new&lt;/span&gt; RenderModel(content, CultureInfo.CurrentUICulture);

    &lt;span class="rem"&gt;//add an umbraco data token so the umbraco view engine executes&lt;/span&gt;
    RouteData.DataTokens[&lt;span class="str"&gt;"umbraco"&lt;/span&gt;] = model;

    &lt;span class="kwrd"&gt;return&lt;/span&gt; model;
}&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
The first thing is that you need to construct the &lt;em&gt;RenderModel&lt;/em&gt; with an explicit culture otherwise you’ll get an exception. The next line adds the created &lt;em&gt;RenderModel&lt;/em&gt; to the RouteData.DataTokens… this is because we want to render an Umbraco view which will be stored in either of the following places (based on Umbraco standard practices):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;~/Views/Product.cshtml&lt;/li&gt;
&lt;li&gt;~/Views/ProductCategory.cshtml&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;These locations are not MVC standard practices. Normally MVC will look in a controller specific folder for views. For this custom controller the locations would be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;~/Views/MyProduct/Product.cshtml&lt;/li&gt;
&lt;li&gt;~/Views/MyProduct/ProductCategory.cshtml&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;But we want to use the views that Umbraco has created for us so we need to ensure that the built in Umbraco ViewEngine gets executed. For performance reasons the Umbraco &lt;em&gt;RenderViewEngine&lt;/em&gt; will not execute for a view unless a &lt;em&gt;RenderModel&lt;/em&gt; instance exists in the RouteData.DataTokens with a key of “umbraco”, so we just add it there before we return the view.&lt;/p&gt;
&lt;h2&gt;Views&lt;/h2&gt;
&lt;p&gt;The views are your regular Umbraco views but there’s a few things that might not work:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Macros. Sorry, since we’ve bypassed the Umbraco routing pipeline which macros rely upon, any call to Umbraco.RenderMacro will fail. But you should be able to achieve what you want with Partial Views or Child Actions.&lt;/li&gt;
&lt;li&gt;Umbraco.Field. Actually this will work but you’ll need to upgrade to 6.0.7 or 6.1.2 based on this fixed issue: &lt;a href="http://issues.umbraco.org/issue/U4-2324"&gt;http://issues.umbraco.org/issue/U4-2324&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;One cool thing is that you can use the regular MVC &lt;em&gt;UrlHelper&lt;/em&gt; to resolve the URLs of your actions, since this custom controller is actually just a regular old MVC controller after all.&lt;/p&gt;
&lt;p&gt;These view example are nothing extraordinary, just demonstrating that they are the same as Umbraco templates with the same model (but using our custom URLs)&lt;/p&gt;
&lt;h3&gt;ProductCategory&lt;/h3&gt;&lt;pre class="csharpcode"&gt;@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;
}
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Product category&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@Model.Content.Name&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            @foreach (var product in Model.Content.Children
                .Where(x =&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; x.DocumentTypeAlias == "Product"))
            {
                &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;="@Url.Action("&lt;/span&gt;&lt;span class="attr"&gt;Product&lt;/span&gt;&lt;span class="kwrd"&gt;", "&lt;/span&gt;&lt;span class="attr"&gt;MyProduct&lt;/span&gt;&lt;span class="kwrd"&gt;", new { id = product.Name })"&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                        @product.Name
                    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;a&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;li&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            }
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;ul&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;p&gt;Which looks like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_36.png"&gt;&lt;img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_34.png" width="244" height="201"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;Product&lt;/h3&gt;&lt;pre class="csharpcode"&gt;@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;
}
&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Product&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h1&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;hr&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;@Model.Content.Name&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;h2&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            @(Model.Content.GetPropertyValue("Description"))
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;div&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;body&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;html&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
Which looks like this:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://shazwazza.com/media/articulate-import/image_37.png"&gt;&lt;img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="https://shazwazza.com/media/articulate-import/image_thumb_35.png" width="244" height="187"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Whats next?&lt;/h2&gt;
&lt;p&gt;With the setup above you should be able to achieve most of what you would want with custom routing, controllers, URLs and lookups. However, as I mentioned before things like executing Macros and potentially other internal Umbraco bits that rely on objects like the &lt;em&gt;PublishedContentRequest&lt;/em&gt; will not work. &lt;/p&gt;
&lt;p&gt;Of course if there is a will, there is a way and I have some cool ideas that could make all of those things work seamlessly too with custom MVC routes. Stay tuned!&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1163</guid>
      <link>https://shazwazza.com/post/umbraco-jupiter-plugins-part-5-surface-controllers/</link>
      <category>Umbraco</category>
      <title>Umbraco Jupiter Plugins - Part 5 - Surface Controllers</title>
      <description>&lt;p&gt;This is the fifth blog post in a series of posts relating to building plugins for Umbraco v5 (Jupiter). This post will explain what a Surface Controller is, what they can be used for and how to create one. &lt;/p&gt; &lt;h2&gt;Disclaimer&lt;/h2&gt; &lt;p&gt;&lt;em&gt;This post is about developing for Umbraco v5 (Jupiter) which at the time of this post is still under development. The technical details described below may change by the time Umbraco Jupiter is released. If you have feedback on the technical implementation details, please comment below.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Related Posts:&lt;/strong&gt;  &lt;ol&gt; &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-1.aspx"&gt;Umbraco Jupiter Plugins – Part 1&lt;/a&gt;  &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-2-Routing.aspx"&gt;Umbraco Jupiter Plugins – Part 2 – Routing&lt;/a&gt;  &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-3-Trees.aspx"&gt;Umbraco Jupiter Pluings – Part 3 – Trees&lt;/a&gt;  &lt;li&gt;&lt;a href="http://shazwazza.com/post/Umbraco-Jupiter-Plugins-Part-4-Editors.aspx"&gt;Umbraco Jupiter Pluings – Part 4 – Editors&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h1&gt;What is a Surface Controller?&lt;/h1&gt; &lt;p&gt;A Surface Controller is an MVC controller that interacts with the front-end (or render layer) of Umbraco. An example of a Surface Controller could be a controller that has a &lt;a href="http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx" target="_blank"&gt;Child Action&lt;/a&gt; used to display a Twitter Feed, or a controller that has an Action to accept some posted information from a form. Child Actions on Surface Controller will probably be primarily used for Child Action Macros in Umbraco v5. &lt;/p&gt; &lt;p&gt;Since Surface Controllers are plugins, this means that you can create a package that contains Surface Controllers to distribute whatever front-end functionality you like to Umbraco developers. Surface Controllers, just like Tree Controllers and Editor Controllers get automatically routed for you.&lt;/p&gt; &lt;h1&gt;Creating a Surface Controller&lt;/h1&gt; &lt;p&gt;&lt;em&gt;&lt;strong&gt;Important convention: &lt;/strong&gt;All Surface controller names MUST be suffixed with ‘SurfaceController’. For example, if you are creating a Surface Controller to display system a Twitter feed, you might call your controller: TwitterFeed&lt;strong&gt;SurfaceController&lt;/strong&gt;. If you don’t follow this convention, you’re surface controller wont be routed.&lt;/em&gt;&lt;/p&gt; &lt;p&gt;The first step is to create a class that inherits from the base Surface Controller class:&amp;nbsp; &lt;strong&gt;Umbraco.Cms.Web.Surface.SurfaceController &lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The next step is to define some MVC Action’s to do whatever it is you’d like them to do. Here’s some examples:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Creating an action to partake in a Child Action Macro. To define this is very simple and follows the exact same MVC principles to creating a Child Action… just add a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.childactiononlyattribute.aspx" target="_blank"&gt;ChildActionOnlyAttribute&lt;/a&gt; to your action method:&lt;/li&gt;&lt;/ul&gt;&lt;pre class="csharpcode"&gt;[ChildActionOnly]
&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult DisplayTwitterFeed()&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;ul&gt;
&lt;li&gt;Creating a child action to simply be used as a normal MVC child action which get rendered using @Html.Action or @Html.RenderAction 
&lt;li&gt;Create an action to handle some posted form data:&lt;/li&gt;&lt;/ul&gt;&lt;pre class="csharpcode"&gt;[HttpPost]
&lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult HandleMyFormSubmission(MyFormModel model) &lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;ul&gt;
&lt;li&gt;Maybe you’d like to track all links clicked on your page. You could use jquery to update all of your links on your page to point to your custom action URL with the real URL as a query string. Then your custom action will log the real link address and redirect the user to where they want to go.&lt;/li&gt;&lt;/ul&gt;
&lt;h2&gt;Plugins vs Non-plugins&lt;/h2&gt;
&lt;p&gt;A Surface Controller ‘can’ be a plugin, meaning that you can create it as a plugin and distribute it as part of a package. However, if you are creating your own Umbraco website and do your development in Visual Studio like most of us, you don’t need to create a Surface Controller with a plugin definition and install it as part of a package, you can just define it locally just like a controller in a normal MVC project. If you do want to ship your Surface Controller as part of a package then you must attribute your Surface Controller with the SurfaceAttribute, and give it an Id. If you don’t do this then Umbraco will detect that its loading a Surface Controller plugin without an Id and throw an exception.&lt;/p&gt;
&lt;h2&gt;As a plugin&lt;/h2&gt;
&lt;p&gt;Standard practice for creating any kind of controller is to put your controllers in the ‘Controllers’ folder (this is not mandatory but a simple convention to follow). So If you’ve created a new project in Visual Studio, you’d create a folder called ‘Controllers’, then create your Surface Controller class with the SurfaceAttribute and an Id:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.Mvc;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Umbraco.Cms.Web.Context;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Umbraco.Cms.Web.Surface;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Umbraco.Framework;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; MyProject.Controllers
{
    [Surface(&lt;span class="str"&gt;"98625300-6DF0-41AF-A432-83BD0232815A"&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TwitterFeedSurfaceController : SurfaceController
    {

    }
}&lt;/pre&gt;
&lt;p&gt;Because this Surface Controller is a plugin, you’ll need to attribute your project assembly (just like when creating Trees or Editor plugins). You can declare this in any of your classes or in the AssemblyInfo.cs file.&lt;/p&gt;&lt;pre class="csharpcode"&gt;[assembly: AssemblyContainsPlugins]&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;h2&gt;As a locally declared Surface Controller&lt;/h2&gt;
&lt;p&gt;This is pretty much identical to the above but you don’t have to include the SurfaceAttribute or attribute your assembly. If you’ve got an Umbraco v5 website that you’re working on you should just create a ~/Controllers folder to put your controller in, just as if you were creating a normal MVC project. Then you can create your Surface Controller:&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.Mvc;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Umbraco.Cms.Web.Context;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Umbraco.Cms.Web.Surface;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Umbraco.Framework;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; MyProject.Controllers
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TwitterFeedSurfaceController : SurfaceController
    {

    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;

&lt;h1&gt;Using a Surface Controller&lt;/h1&gt;
&lt;p&gt;The usage of a Surface Controller really depends on what you’ve created your surface controller to do. Probably the 3 main ways to use them will be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a ChildAction Macro by using the Macro UI in the back office and selecting a child action that you’ve declared on your Surface Controller, then render the macro in your template or inline in the WYSIWYG editor. 
&lt;li&gt;Directly render a child action declared on your Surface Controller by using &lt;em&gt;@Html.Action &lt;/em&gt;or &lt;em&gt;@Html.RenderAction&lt;/em&gt; 
&lt;li&gt;Create an Html form to post data to an action on your Surface Controller using &lt;em&gt;@Html.BeginUmbracoForm &lt;/em&gt;(more on this in the next blog post!)&lt;/li&gt;&lt;/ul&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1168</guid>
      <link>https://shazwazza.com/post/taming-the-buildmanager-aspnet-temp-files-and-appdomain-restarts/</link>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>Taming the BuildManager, ASP.Net Temp files and AppDomain restarts</title>
      <description>&lt;p&gt;I’ve recently had to do a bunch of research in to the BuildManager and how it deals with caching assemblies since my involvement with creating an &lt;a href="http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx"&gt;ASP.Net plugin engine&lt;/a&gt;. Many times people will attempt to restart their AppDomain by ‘bumping’ their web.config files, meaning adding a space character or carriage return and then saving it. Sometimes you may have noticed that this does not always restart your AppDomain the way you’d expect and some of the new types you’d expect to have loaded in your AppDomain simply aren’t there. This situation has cropped up a few times when using the plugin engine that we have built in to &lt;a href="http://umbraco.com" target="_blank"&gt;Umbraco&lt;/a&gt; v5 and some people have resorted to manually clearing out their ASP.Net temp files and then forcing an IIS Restart, but this is hardly ideal. The good news is that we do have control over how these assemblies get refreshed, in fact the BuildManager reloads/refreshes/clears assemblies in different ways depending on how the AppDomain is restarted.&lt;/p&gt; &lt;h2&gt;The hash.web file&lt;/h2&gt; &lt;p&gt;An important step during the BuildManager initialization phase is a method call to &lt;em&gt;BuildManager.CheckTopLevelFilesUpToDate&lt;/em&gt; which does some interesting things. First it checks if a special hash value is on disk and is not zero. You may have noticed a file in your ASP.Net temp files: /hash/hash.web and it will contain a value such as: 4f34227133de5346. This value represents a unique hash of many different combined objects that the BuildManager monitors. If this file exists and the value can be parsed properly then the BuildManager will call: &lt;em&gt;diskCache.RemoveOldTempFiles(); &lt;/em&gt;What this does is the following:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;removes the CodeGen temp resource folder&lt;/li&gt; &lt;li&gt;removes temp files that have been saved during CodeGen such as *.cs &lt;/li&gt; &lt;li&gt;removes the special &lt;em&gt;.delete &lt;/em&gt;files and their associated original files which have been created when shutting down the app pool and when the .dll files cannot be removed (due to file locking)&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Creating the hash value&lt;/h2&gt; &lt;p&gt;The next step is to create this hash value based on the current objects in the AppDomain. This is done using an internal utility class in the .Net Framework called: &lt;em&gt;HashCodeCombiner&lt;/em&gt; (pretty much everything that the BuildManager references is marked Internal! ). The process combines the following object values to create the hash (I’ve added in parenthesis the actual properties the BuildManager references):&lt;/p&gt; &lt;ul&gt; &lt;li&gt;the app's physical path, in case it changes &lt;em&gt;(HttpRuntime.AppDomainAppPathInternal)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;System.Web.dll &lt;em&gt;(typeof(HttpRuntime).Module.FullyQualifiedName)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;machine.config file name &lt;em&gt;(HttpConfigurationSystem.MachineConfigurationFilePath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;&lt;strong&gt;root &lt;/strong&gt;web.config file name, please note that this is not your web apps web.config &lt;em&gt;(HttpConfigurationSystem.RootWebConfigurationFilePath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the hash of the &amp;lt;compilation&amp;gt; section &lt;em&gt;(compConfig.RecompilationHash)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the hash of the system.web/profile section &lt;em&gt;(profileSection.RecompilationHash)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the file encoding set in config &lt;em&gt;(appConfig.Globalization.FileEncoding)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;the &amp;lt;trust&amp;gt; config section &lt;em&gt;(appConfig.Trust.Level &amp;amp; appConfig.Trust.OriginUrl)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;whether profile is enabled &lt;em&gt;(ProfileManager.Enabled)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;whether we are precompiling with debug info (but who precompiles :) &lt;em&gt;(PrecompilingWithDebugInfo)&lt;/em&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Then we do a check for something I didn’t know existed called &lt;a href="http://blogs.msdn.com/b/davidebb/archive/2009/04/15/a-new-flag-to-optimize-asp-net-compilation-behavior.aspx" target="_blank"&gt;&lt;em&gt;Optimize Compilations&lt;/em&gt;&lt;/a&gt;&lt;em&gt;&amp;nbsp;&lt;/em&gt;which will not actual affect the hash file value for the following if it is set to true (by default is is false):&lt;/p&gt; &lt;ul&gt; &lt;li&gt;the ‘bin’ folder &lt;em&gt;(HttpRuntime.BinDirectoryInternal)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;App_WebReferences &lt;em&gt;(HttpRuntime.WebRefDirectoryVirtualPath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;App_GlobalResources&amp;nbsp; &lt;em&gt;(HttpRuntime.ResourcesDirectoryVirtualPath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;App_Code &lt;em&gt;(HttpRuntime.CodeDirectoryVirtualPath)&lt;/em&gt;&lt;/li&gt; &lt;li&gt;Global.asax &lt;em&gt;(GlobalAsaxVirtualPath)&lt;/em&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Refreshing the ASP.Net temp files (CodeGen files)&lt;/h2&gt; &lt;p&gt;The last step of this process is to check if the persisted hash value in the hash.web file equals the generated hash value from the above process. If they do not match then a call is made to &lt;em&gt;diskCache.RemoveAllCodegenFiles(); &lt;/em&gt;which will:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;clear all codegen files, removes all files in folders but not the folders themselves, &lt;/li&gt; &lt;li&gt;removes all root level files except for temp files that are generated such as .cs files, etc...&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;This essentially clears your ASP.Net temp files completely, including the MVC controller cache file, etc…&lt;/p&gt; &lt;p&gt;Then the BuildManager simply resaves this calculated has back to the hash.web file.&lt;/p&gt; &lt;h2&gt;What is the best way to restart your AppDomain?&lt;/h2&gt; &lt;p&gt;There is really no ‘best’ way, it just depends on your needs. If you simply want to restart your AppDomain and not have the overhead of having your app recompile all of your views and other CodeGen classes then it’s best that you simply ‘bump’ your web.config by just adding a space, carriage return or whatever. As you can see above the hash value is not dependant on your local web.config file’s definition (timestamp, etc…). However, the hash value is dependent on some other stuff in your apps configuration such as the &amp;lt;compilation&amp;gt; section, system.web/profile section, the file encoding configured, and the &amp;lt;trust&amp;gt; section. If you update any value in any of these sections in your web.config it will force the BuildManager to clear all cached CodeGen files which is the same as clearing your ASP.Net temp files.&lt;/p&gt; &lt;p&gt;So long as you don’t have optimizeCompilations set to true, then the easiest way to clear your CodeGen files is to simply add a space or carriage return to your global.asax file or modify something in the 4 other places that the BuildManager checks locally: App_Code, App_GlobalResources, App_WebResources, or modify/add/remove a file in your bin folder.&lt;/p&gt; &lt;h2&gt;How does this affect the &lt;a href="http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx"&gt;ASP.Net plugin engine&lt;/a&gt;?&lt;/h2&gt; &lt;p&gt;Firstly, i need to update that post as the code in the plugin engine has changed quite a bit but you can find the latest in the &lt;a href="http://umbraco.codeplex.com/SourceControl/list/changesets" target="_blank"&gt;Umbraco source code on CodePlex&lt;/a&gt;. With the above knowledge its easy to clear out any stale plugins by perhaps bumping your global.asax file, however this is still not an ideal process. With the research I’ve done in to the BuildManager I’ve borrowed some concepts from it and learned of a special “.delete” file extension that the BuildManager looks for during AppDomain restarts. With this new info, I’ve already committed some new changes to the PluginManager so that you shouldn’t need to worry about stale plugin DLLs.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1174</guid>
      <link>https://shazwazza.com/post/umbraco-jupiter-plugins-part-1/</link>
      <category>ASP.Net</category>
      <category>Umbraco</category>
      <title>Umbraco Jupiter Plugins - Part 1</title>
      <description>&lt;p&gt;This is the first blog post in a series of posts relating to building plugins for Umbraco v5 (Jupiter)&lt;/p&gt;  &lt;h2&gt;Disclaimer&lt;/h2&gt;  &lt;p&gt;&lt;em&gt;This post is about developing for Umbraco v5 (Jupiter) which at the time of this post is still under development. The technical details described below may change by the time Umbraco Jupiter is released. If you have feedback on the technical implementation details, please comment below.&lt;/em&gt;&lt;/p&gt;  &lt;h1&gt;What is a plugin&lt;/h1&gt;  &lt;p&gt;Since Umbraco Jupiter has been built from the ground up, we first need to define some v5 terminology:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Plugin&lt;/strong&gt; = A single functional component that extends Umbraco such as a Tree, an Editor, a Menu Item, etc… &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Package&lt;/strong&gt; = A group of Plugins installed into Umbraco via the ~/Plugins/Packages/&lt;em&gt;[Package Name]&lt;/em&gt; folder &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The Umbraco v5 back-office has been architected to run entirely on Plugins, in fact the core trees, editors, etc… that are shipped with Umbraco are Plugins in v5.&lt;/p&gt;  &lt;h1&gt;Types of plugins&lt;/h1&gt;  &lt;p&gt;The list of Plugin types will most likely increase from the time of this blog post to when Jupiter is launched but as it stands now here are the types of Plugins supported in v5:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Property Editors &lt;/strong&gt;      &lt;ul&gt;       &lt;li&gt;This term is new to v5. In v4.x there was no differentiation between a &lt;em&gt;Data Type&lt;/em&gt; and it’s underlying ‘Data Type’ editor. In v5 we’ve made a clear distinction between a ‘Data Type’ (as seen in the &lt;em&gt;Data Type&lt;/em&gt; tree and used as properties for &lt;em&gt;Document Types&lt;/em&gt;) and the underlying editor and pre-value editor that it exposes.&amp;#160; An example of a &lt;em&gt;Property Editor&lt;/em&gt; would be &lt;a href="http://ucomponents.codeplex.com" target="_blank"&gt;uComponents’&lt;/a&gt; Multi-Node Tree Picker. An example of a &lt;em&gt;Data Type&lt;/em&gt; would be when an Umbraco administrator creates a new &lt;em&gt;Data Type &lt;/em&gt;node in the &lt;em&gt;Data Type &lt;/em&gt;tree and assigns the Multi-Node Tree Picker as it’s &lt;em&gt;Property Editor&lt;/em&gt;. &lt;/li&gt;        &lt;li&gt;So uComponents Team, this is where you need to focus your efforts for version 5! &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Menu Items&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;Context menu items such as Create, Publish, Audit Trail, etc… are all plugins. &lt;/li&gt;        &lt;li&gt;See &lt;a href="http://shazwazza.com/post/Custom-Actions-in-Umbraco-Jupiter.aspx" target="_blank"&gt;this post&lt;/a&gt; in regards to creating menu items in v5, though there have been some new features added since that article was created which I’ll detail in coming posts in this series. &lt;!--EndFragment--&gt;&lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Editors&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;Editor plugins are all of the interactions performed in the right hand editor panel and in modal dialogs in the back-office. &lt;/li&gt;        &lt;li&gt;For example, the Content Editor core plugin in v5 manages the rendering for all views such as editing content, sorting, copying, and moving nodes, and nearly all of the other views that the context menu actions render. &lt;/li&gt;        &lt;li&gt;Editors are comprised mostly of MVC Controllers, Views, JavaScript &amp;amp; CSS. &lt;!--EndFragment--&gt;&lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Trees&lt;/strong&gt;       &lt;ul&gt;       &lt;li&gt;All trees are plugins, for example, the Content tree, Media tree, Document Type tree are all different plugins. &lt;/li&gt;        &lt;li&gt;Trees are actually MVC controllers. &lt;/li&gt;        &lt;li&gt;Tree nodes use &lt;em&gt;Menu Items&lt;/em&gt; to render &lt;em&gt;Editors&lt;/em&gt; &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;&amp;#160;&lt;strong&gt;Presentation Addins (Yet to be officially named)&lt;/strong&gt; &lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Another type of plugin are Controllers that are installed via packages that interact with the presentation layer, an example of this might be the Controller Action that accepts the post values from a Contour form. &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;h1&gt;Whats next?&lt;/h1&gt;  &lt;p&gt;In the coming blog posts I’ll talk about &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;how plugins are installed and loaded &lt;/li&gt;    &lt;li&gt;how the Umbraco routing system works with all of these controllers &lt;/li&gt;    &lt;li&gt;how and where the Views are stored that the plugins reference &lt;/li&gt;    &lt;li&gt;how to create all of the different types of plugins &lt;/li&gt; &lt;/ul&gt;  &lt;h1&gt;Code Garden 2011&lt;/h1&gt;  &lt;p&gt;I’ll be doing a talk on &lt;a href="http://codegarden11.com/sessions/day-2/slot-one/get-plugged-in-to-umbraco-jupiter.aspx" target="_blank"&gt;Plugins for Umbraco Jupiter&lt;/a&gt; at &lt;a href="http://codegarden11.com/" target="_blank"&gt;Code Garden 2011&lt;/a&gt; this year which will go in to a lot more detail than these blog posts. If you are attending Code Garden already, then hopefully this series will give you a head start on Umbraco v5. If you haven’t got your tickets to Code Garden yet, what are you waiting for?! We have so much information to share with you :)&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
    <item>
      <guid isPermaLink="false">1326</guid>
      <link>https://shazwazza.com/post/razor-plus-dynamic-plus-internal-plus-interface-the-object-does-not-contain-a-definition-for-xxxx-exception/</link>
      <category>ASP.Net</category>
      <category>Web Development</category>
      <title>Razor + dynamic + internal + interface &amp; the 'object' does not contain a definition for 'xxxx' exception</title>
      <description>&lt;p&gt;I’ve come across this strange issue and decided to blog about it since I can’t figure out exactly why this is happening it is clearly something to do with the DLR and interfaces.&lt;/p&gt; &lt;p&gt;First, if you are getting the exception:&lt;em&gt; 'object' does not contain a definition for 'xxxx'&lt;/em&gt; it is related to either an object you have being marked internal or you are are using an anonymous object type for your model (which .Net will always mark as internal). &lt;/p&gt; &lt;p&gt;Here’s a really easy way to replicate this:&lt;/p&gt; &lt;p&gt;1. Create an internal model&lt;/p&gt; &lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; height: 80px; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 38.17%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyModel&lt;br&gt;{&lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;set;}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;2. Return this model in your MVC action&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult Index()&lt;br&gt;{            &lt;br&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; View(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InternalTestModel(&lt;span style="color: #006080"&gt;"Shannon"&lt;/span&gt;));&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;3. Make your view have a dynamic model and then try to render the model’s property&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;@model dynamic&lt;br&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;@Model.Name&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;You’ll get the exception:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Server Error in '/' Application. &lt;/em&gt;
&lt;p&gt;&lt;em&gt;'object' does not contain a definition for 'Name'&lt;/em&gt; 
&lt;p&gt;So even though the error is not very informative, it makes sense since Razor is trying to access an internal class. &lt;/p&gt;
&lt;h2&gt;Try using a public interface&lt;/h2&gt;
&lt;p&gt;Ok so if we want to keep our class internal, we could expose it via a public interface. Our code might then look like this:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IModel &lt;br&gt;{    &lt;br&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;}&lt;br&gt;}&lt;br&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyModel : IModel &lt;br&gt;{        &lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;set;}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;Then we can change our view to be strongly typed like:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;@model IModel&lt;br&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;@Model.Name&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;h1&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;And it will work, however if you change your view back to be &lt;em&gt;@model dynamic&lt;/em&gt; you &lt;strong&gt;will still get the exception&lt;/strong&gt;. Pretty sure it’s because the DLR is just binding to the instance object and doesn’t really care about the interface… this makes sense.&lt;/p&gt;
&lt;h2&gt;Try using an abstract public class&lt;/h2&gt;
&lt;p&gt;For some reason if you make your internal class inherit from a public abstract class that implements the same interface you will not get this exception even though the object instance you are passing to razor is internal. For example with this structure:&lt;/p&gt;
&lt;div id="codeSnippetWrapper" style="overflow: auto; cursor: text; font-size: 8pt; border-top: silver 1px solid; font-family: 'Courier New', courier, monospace; border-right: silver 1px solid; border-bottom: silver 1px solid; padding-bottom: 4px; direction: ltr; text-align: left; padding-top: 4px; padding-left: 4px; margin: 20px 0px 10px; border-left: silver 1px solid; line-height: 12pt; padding-right: 4px; max-height: 200px; width: 97.5%; background-color: #f4f4f4"&gt;&lt;pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IModel &lt;br&gt;{        &lt;br&gt;    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;}&lt;br&gt;}&lt;br&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;abstract&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; ModelBase : IModel &lt;br&gt;{&lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;abstract&lt;/span&gt; Name {get;}&lt;br&gt;}&lt;br&gt;&lt;span style="color: #0000ff"&gt;internal&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MyModel : IModel &lt;br&gt;{            &lt;br&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Name {get;set;}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;
&lt;p&gt;You &lt;strong&gt;will not&lt;/strong&gt; get this error if your view is &lt;em&gt;@model dynamic. &lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;Strange!&lt;/h2&gt;
&lt;p&gt;I’m sure there’s something written in the DLR spec about this but still seems strange to me! If you are getting this error and you aren’t using anonymous objects for your model, hopefully this might help you figure out what is going on.&lt;/p&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:08:08 Z</pubDate>
      <a10:updated>2023-03-23T15:08:08Z</a10:updated>
    </item>
  </channel>
</rss>