簡體   English   中英

IIS URL重寫模塊:獲取ApplicationPath

[英]IIS Url Rewrite Module: Get ApplicationPath

我正在尋找一種方法來重寫url,以防url中的應用程序路徑具有不同的大小寫。 由於不同部署的應用程序路徑可能不同,因此我需要動態訪問它。 這有什么辦法嗎?

背景:

我正在設置cookie到應用程序路徑的路徑。 由於cookie路徑區分大小寫,我需要重寫URL以防它們被錯誤地裝入。 我還想有其他方法,不需要使用url重寫模塊。

假設對於一個部署,應用程序的別名是“ApplicationA”(對於另一個部署,別名可能是“ApplicationB”)。

http://<host>:<port>/<applicationA or Applicationa or APPLicationA etc.>/<rest of the url>

Redirect to 

http://<host>:<port>/ApplicationA/<rest of the url>

不確定REWRITE在你的情況下是否正確運行,也許你應該使用REDIRECT(永久),但是下面的規則允許我在特定情況下獲取應用程序名稱:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="My Rule" stopProcessing="true">
                <match url="^(.+)" ignoreCase="false" />
                <conditions>
                    <add input="{REQUEST_URI}" pattern="TmP/.+" ignoreCase="false" negate="true" />
                    <add input="{REQUEST_URI}" pattern="tmp/(.+)" ignoreCase="true" />
                </conditions>
                <action type="Redirect" url="TmP/{C:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

我認為創建和添加自定義Http模塊可以解決您的問題。 響應BeginRequestEndRequest事件,在每個請求上調用HTTP模塊。

您可以動態訪問URL並通過更改其大小寫重定向它。

private void PreRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
        var f = req.Url;
        f="Change case of URL";
        if (condition)
        {

            app.Response.Redirect(f);
        }

    }

更新

我建議您使用HTTPModule訂閱BeginRequest事件。

使用RewritePath方法,您可以獲得Redirect速度,如果外殼錯誤,您只需匹配並重寫網址......或者實際上只是調整外殼可能比先檢查它然后調整(測試並查看之前)你選擇解決方案)。

一個積極的副作用是你可以輕松地進行其他測試並做出例外等。

public class AdjustCasingModule : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.BeginRequest += OnBeginRequest;
    }

    protected void BeginRequest(object sender, EventArgs e)
    {
        var httpContext = ((HttpApplication)sender).Context;

        string[] path = httpContext.Request.Path.Split('/');

        if (path[1].Length > 0)
        {
            Regex rgx = new Regex(@"^[A-Z][a-z]*[A-Z]");

            if (!rgx.IsMatch(path[1]))
            {
                char[] a = path[1].ToLower().ToCharArray();
                a[0] = char.ToUpper(a[0]);
                a[char.Length - 1] = char.ToUpper(a[char.Length - 1]);
                path[1] = new string(a);
                httpContext.RewritePath(String.Join('/', path));
            }
        }
    }

    public void Dispose()
    {

    }
}

邊注:

我仍然建議首先使用小寫路徑。

只是一個想法,如果有人會考慮放棄使用駝峰案例表示法(ApplicationA)而強制使用例如小寫*(applicationa),您可以使用ToLower關鍵字,如下所示。

<system.webServer>
    <rewrite>
        <rules>
            <rule name="force lowercase" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
                </conditions>
                <action type="Redirect" url="{ToLower:{URL}}" redirectType="Temporary" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

*如果您在網址中承諾使用原始的camelCase表示法,那么我將遵循Uriil的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM