自定义条件可以帮助您扩展UrlRewriter.NET 的功能:
下面是创建自定义条件的步骤和方法:
/// <summary>
/// Condition that tests the existence of a file.
/// </summary>
internal class ExistsCondition : IRewriteCondition
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="location"></param>
public ExistsCondition(string location)
{
_location = location;
}
/// <summary>
/// Determines if the condition is matched.
/// </summary>
/// <param name="context">The rewriting
context.</param>
/// <returns>True if the condition is met.</returns>
public bool IsMatch(RewriteContext context)
{
string filename = HttpContext.Current.Server.
MapPath(context.Expand(_location));
return File.Exists(filename) ||
Directory.Exists(filename);
}
private string _location;
}
/// <summary>
/// Parser for exists conditions.
/// </summary>
internal class ExistsConditionParser : IRewriteConditionParser
{
/// <summary>
/// Parses the condition.
/// </summary>
/// <param name="node">The node to parse.</param>
/// <returns>The condition parsed, or null
if nothing parsed.</returns>
public IRewriteCondition Parse(XmlNode node)
{
XmlNode existsAttr = node.Attributes.
GetNamedItem("mycondition");
if (existsAttr != null)
{
return new
ExistsCondition(existsAttr.Value);
}
return null;
}
}
<register parser="MyNamespace.Parsers.ExistsConditionParser, MyAssembly" />
<if mycondition="something">
....
</if>