Custom actions allow you to extend the UrlRewriter.NET component with your own actions.

Follow these steps to create your custom action:

  1. Create a class that implements the IRewriteAction interface. This class will be executed when the action is required.
    public class SetStatusAction : IRewriteAction
    {
    /// <summary>
    /// Default constructor.
    /// </summary>
    /// <param name="statusCode">The status code to set.</param>
    public SetStatusAction(HttpStatusCode statusCode)
    {
    _statusCode = statusCode;
    }
    
    /// <summary>
    /// The status code.
    /// </summary>
    public HttpStatusCode StatusCode
    {
    get
    {
    return _statusCode;
    }
    set
    {
    _statusCode = value;
    }
    }
    
    /// <summary>
    /// Executes the action.
    /// </summary>
    /// <param name="context">The rewriting context.</param>
    public void Execute(RewriteContext context)
    {
    context.StatusCode = StatusCode;
    }
    
    /// <summary>
    /// The Processing directive.
    /// </summary>
    public RewriteProcessing Processing
    {
    get
    {
    if ((int)StatusCode >= 300)
    {
    return RewriteProcessing.StopProcessing;
    }
    else
    {
    return RewriteProcessing.ContinueProcessing;
    }
    }
    }
    
    private HttpStatusCode _statusCode;
    }
  2. Create an action parser class that implements the IRewriteActionParser interface. This class will be called by UrlRewriter.NET when reading the rewriting rules and it recognises your action name.
    /// <summary>
    /// Action parser for the set-status action.
    /// </summary>
    public class SetStatusActionParser : IRewriteActionParser
    {
    /// <summary>
    /// Default constructor.
    /// </summary>
    public SetStatusActionParser()
    {
    }
    
    /// <summary>
    /// Parses the node.
    /// </summary>
    /// <param name="node">The node to parse.</param>
    /// <param name="config">The rewriter configuration.
    </param>
    /// <returns>The parsed action, or null if no action 
    parsed.</returns>
    public IRewriteAction Parse(XmlNode node, object config)
    {
    XmlNode statusCodeNode = node.Attributes. 
    GetNamedItem("status");
    if (statusCodeNode == null)
    {
    return null;
    }
    
    return new SetStatusAction((HttpStatusCode) 
    Convert.ToInt32(statusCodeNode.Value));
    }
    
    /// <summary>
    /// Whether the action allows nested actions.
    /// </summary>
    public override bool AllowsNestedActions
    {
    get
    {
    return false;
    }
    }
    
    /// <summary>
    /// Whether the action allows attributes.
    /// </summary>
    public override bool AllowsAttributes
    {
    get
    {
    return true;
    }
    }
    
    public string Name
    {
    get
    {
    return "myaction";
    }
    }
    }
  3. Register your custom action parser.
    <register parser="MyNamespace.Parsers.SetStatusActionParser,
    MyAssembly" />
  4. Use your custom action.
    <myaction status="405" />

See Also