我基本需要的是管理大量的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