Write your own UI Rewrite Template Extension!

The GoLive release of URL Rewrite Module is now available and has a lot of improvement thanks to the IIS Community feedback (and tons of internal brainstorm meetings!). A new feature is the ability to create Rewrite Rules by using Rewrite Templates, Rewrite Templates are UI extensions that help to simplify the creation of rules.

URL Rewrite Module includes 3 templates and more interesting, it allows you to add your own Rewrite Template.

Take a look to the following walkthrough if you haven’t play with Rewrite Templates:

http://learn.iis.net/page.aspx/497/user-friendly-url---rule-template/

I want to show you how easy is to write your own Rewrite Template extension, here is how this demo should look like:

RewriteTemplateBasic

As you know, iis.net has already some good articles that explain how to create an IIS Manager UI Module, so allow me to reuse the following article:

http://learn.iis.net/page.aspx/269/how-to-create-a-simple-iis-manager-module/

Follow the article above and once you are done with it, please do the following changes.

  1. Add the assembly Microsoft.Web.Management.Rewrite.Client.dll to your C# project. If you can’t find the assembly reference in the GAC, download it from here
  2. Replace the code in DemoModule.cs by the following code that registers your extension in the URL Rewrite UI Module:
      using System;
      using System.Windows.Forms;
      using Microsoft.Web.Management.Client;
      using Microsoft.Web.Management.Server;
      using Microsoft.Web.Management.Iis.Rewrite.RewriteTemplates;

      namespace ExtensibilityDemo
      {
       
      internal sealed class DemoModule : Module
       
      {
         
      protected override void Initialize(IServiceProvider serviceProvider, ModuleInfo moduleInfo)
         
      {
           
      base.Initialize(serviceProvider, moduleInfo);

           
      Connection connection = (Connection)GetService(typeof(Connection));

           
      connection.Initialized += delegate
           
      {
             
      Module rewriteModule = connection.Modules["Rewrite"] as Module;
             
      if (rewriteModule == null)
             
      {
               
      // URL Rewrite Module is not installed correctly.
                return;
             
      }

             
      DemoRewriteTemplateFeature demoFeature = 
               
      new DemoRewriteTemplateFeature(rewriteModule);

             
      IExtensibilityManager extensibilityManager = (IExtensibilityManager)
               
      demoFeature.ServiceProvider.GetService(typeof(IExtensibilityManager));

             
      extensibilityManager.RegisterExtension(typeof(RewriteTemplateFeature), demoFeature);
           
      };
         
      }
       
      }
      }

      Colorized by: CarlosAg.CodeColorizer
  3. Add a new file named DemoRewriteTemplateFeature.cs and copy and paste following code:
      using Microsoft.Web.Management.Client;
      using Microsoft.Web.Management.Iis.Rewrite.RewriteTemplates;
      using System.Windows.Forms;

      namespace ExtensibilityDemo
      {
       
      internal sealed class DemoRewriteTemplateFeature : RewriteTemplateFeature
       
      {
         
      private const string FeatureTitle = "Rewrite Extensibility Demo";
         
      private const string FeatureDescription = "Demonstrates the UI Extensibility.";

         
      public DemoRewriteTemplateFeature(Module module)
           
      : base(module, FeatureTitle, FeatureDescription, null, null)
         
      {
         
      }

         
      public override void Run()
         
      {
           
      Form form = new Form();
           
      form.Text = "URL Rewrite Extensibility Demo";
           
      form.StartPosition = FormStartPosition.CenterParent;
           
      form.ShowDialog();
         
      }
       
      }
      }

      Colorized by: CarlosAg.CodeColorizer

Download the demo from here:

To install it, you will need to GAC the assembly RewriteExtensibilityDemo.dll and register the module in administration.config by adding the following entry under the moduleProviders section:

<add name="RewriteExtensibilityDemo"
     type
="ExtensibilityDemo.DemoModuleProvider, RewriteExtensibilityDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8975c3932a2fc4ae" />

No Comments