title: ASP.NET MVC routing based on data store values
body: <p>How would you tackle this problem:  </p>
  
  <p>I have data in my data store. Each item has information about:</p>
  
  <ul>
  <li>URL = an arbitrary number of first route segments that will be used with requests</li>
  <li>some item type = display will be related to this type (read on)</li>
  <li>title = used for example in navigation around my application</li>
  <li>etc.</li>
  </ul>
  
  <p>Since each item can have an arbitrary number of segments, I created a custom route, that allows me to handle these kind of requests without using the default route and having a single greedy route parameter.</p>
  
  <p>Item type will actually define in what way should content of a particular item be displayed to the client. I was thinking of creating just as many controllers to not have too much code in a single controller action.</p>
  
  <p>So how would you do this in ASP.NET MVC or what would you suggest would be the most feasible way of doing this?</p>
  
  <h2>Edit: A few more details</h2>
  
  <p>My items are stored in a database. Since they can have very different types (not inheritable) I thought of creating just as many controllers. But questions arise:</p>
  
  <ol>
  <li><p>How should I create these controllers on each request since they are related to some dynamic data? I could create my own Controller factory or Route handler or possibly some other extension points as well, but which one would be best?</p></li>
  <li><p>I want to use MVC basic functionality of using things like <code>Html.ActionLink(action, controller, linkText)</code> or make my own extension like <code>Html.ActionLink(itemType, linkText)</code> to make it even more flexible, so Action link should create correct routes based on Route data (because that's what's going on in the background - it goes through routes top down and see which one returns a resulting URL).</p></li>
  <li><p>I was thinking of having a configuration of relation between <em>itemType</em> and <em>route values</em> (controller, action, defaults). Defaults setting may be tricky since defaults should be deserialized from a configuration string into an object (that may as well be complex). So I thought of maybe even having a configurable relation between <em>itemType</em> and <em>class type</em> that implements a certain interface like written in the example below.</p></li>
  <li><p>My routes can be changed (or some new ones added) in the data store. But new types should not be added. Configuration would provide these scenarios, because they would link types with route defaults.</p></li>
  </ol>
  
  <p><strong>Example</strong>:</p>
  
  <p>Interface definition:</p>
  
  <pre><code>public interface IRouteDefaults
  {
      object GetRouteDefaults();
  }
  </code></pre>
  
  <p>Interface implementation example:</p>
  
  <pre><code>public class DefaultType : IRouteDefaults
  {
      public object GetRouteDefaults()
      {
          return new {
              controller = "Default",
              action = "Show",
              itemComplex = new Person {
                  Name = "John Doe",
                  IsAdmin = true
              }
      }
  }
  </code></pre>
  
  <p>Configuration example:</p>
  
  <pre><code>&lt;customRoutes&gt;
      &lt;route name="Cars" type="TypeEnum.Car" defaults="MyApp.Routing.Defaults.Car, MyApp.Routing" /&gt;
      &lt;route name="Fruits" type="TypeEnum.Fruit" defaults="MyApp.Routing.Defaults.Fruit, MyApp.Routing" /&gt;
      &lt;route name="Shoes" type="TypeEnum.Shoe" defaults="MyApp.Routing.Defaults.Shoe, MyApp.Routing" /&gt;
      ...
      &lt;route name="Others" type="TypeEnum.Other" defaults="MyApp.Routing.Defaults.DefaultType, MyApp.Routing" /&gt;
  &lt;/customRoutes&gt;
  </code></pre>
  
  <p>To address performance hit I can cache my items and work with in-memory data and avoid accessing the database on each request. These items tend to not change too often. I could cache them for like 60 minutes without degrading application experience.</p>