我基本需要的是管理大量的URL重写和重定向。重定向很容易做到,但重写应该通过IIS中的ARR代理进行代理。据我所知,IIS重写模块使用IIS中的本地setUrl API来获取ARR代理来转发请求。我不愿意编写本地模块,因此我认为可以编写一个托管模块来执行相同的操作。是否可以从托管的HttpModule调用IIS 7+ setUrl API?
@CarlosAg:只是扩大我对你的答案的评论。下面是我需要设置的规则和提供程序,以使重写模块能够按照我的需要进行操作。它只是我自己还是看起来有点像操纵重写模块做一些本不应该做的事情?
<rewrite>
<providers>
<provider name="CustomRewrite" type="MyRewriteProviders.RewriteProvider, MyRewriteProviders, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e72501f8a0edfe78" />
<provider name="CustomRedirectTemporary" type="MyRewriteProviders.RedirectTemporaryProvider, MyRewriteProviders, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e72501f8a0edfe78" />
<provider name="CustomRedirectPermanent" type="MyRewriteProviders.RedirectPermanentProvider, MyRewriteProviders, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e72501f8a0edfe78" />
</providers>
<rules>
<clear />
<rule name="Set Http">
<match url="." />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<serverVariables>
<set name="RequestProtocol" value="http" />
</serverVariables>
<action type="None" />
</rule>
<rule name="Set Https">
<match url="." />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="ON" />
</conditions>
<serverVariables>
<set name="RequestProtocol" value="https" />
</serverVariables>
<action type="None" />
</rule>
<rule name="Custom Rewrite">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CustomRewrite:{RequestProtocol}://{HTTP_HOST}/{URL}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" logRewrittenUrl="true" />
</rule>
<rule name="Custom Redirect (Temporary)" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CustomRedirectTemporary:{RequestProtocol}://{HTTP_HOST}/{URL}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Found" appendQueryString="false" />
</rule>
<rule name="Custom Redirect (Permanent)" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CustomRedirectPermanent:{RequestProtocol}://{HTTP_HOST}/{URL}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
和提供者的代码:
using System;
using System.Collections.Generic;
using Microsoft.Web.Iis.Rewrite;
namespace MyRewriteProviders
{
public enum ActionType
{
Rewrite,
RedirectPermanent,
RedirectTemporary
}
public abstract class BaseProvider : IRewriteProvider
{
protected abstract ActionType Action { get; }
public void Initialize(IDictionary<string, string> settings, IRewriteContext rewriteContext) {}
public string Rewrite(string value)
{
return RewriteEngine.RewriteUri(value, ActionType.Rewrite);
}
}
public class RewriteProvider : BaseProvider
{
protected override ActionType Action
{
get { return ActionType.Rewrite; }
}
}
public class RedirectTemporaryProvider : BaseProvider
{
protected override ActionType Action
{
get { return ActionType.RedirectTemporary; }
}
}
public class RedirectPermanentProvider : BaseProvider
{
protected override ActionType Action
{
get { return ActionType.RedirectPermanent; }
}
}
public static class RewriteEngine
{
public static string RewriteUri(string uri, ActionType action)
{
// Not actual rule engine implementation... just to demonstrate what it would do.
var ub = new UriBuilder(uri);
// Simulate a match on a rewrite rule
if (string.Equals(ub.Host, "rewrite.com", StringComparison.InvariantCultureIgnoreCase) && action == ActionType.Rewrite)
{
ub.Host = "rewrite-rewritten.com";
return ub.ToString();
}
// Simulate a match on a temporary redirect rule
if (string.Equals(ub.Host, "redirect.com", StringComparison.InvariantCultureIgnoreCase) && action == ActionType.RedirectTemporary)
{
ub.Host = "redirect-rewritten.com";
return ub.ToString();
}
// No rules matched. This will make the condition in the rewrite rule not match.
return string.Empty;
}
}
}
从
除此之外,你需要设置IIS允许自定义服务器变量,您需要注册并在海关总署注册的供应商。这似乎只是比简单地处理请求url更复杂一点,就像你是从本机API那里操作一样。
是的,我已经尝试(并成功)在重写模块中使用提供程序可扩展性模型解决问题。然而,解决方案并不是非常...漂亮...可维护...或测试和部署友好。我希望他们会使整个规则引擎可扩展,而不仅仅是模式部分。 – JohannesH 2010-11-03 06:50:33
请参阅上面供应商扩展的代码... – JohannesH 2010-11-03 07:26:40