【问题标题】:Server.MapPath() with IIS URL Rewrite Module 2.0带有 IIS URL 重写模块 2.0 的 Server.MapPath()
【发布时间】:2013-08-12 09:44:00
【问题描述】:

我在 IIS 中有一个网站,将旧版经典 asp 应用程序配置为子应用程序。

我基本上是在尝试创建 URL 重写规则,这样我就不必更改代码中的所有相对 URL。

例如诸如“/themes/somedirectory”之类的 URL 应映射到“/legacy/themes/somedirectory

使用URL Rewrite Module 2.0 我有一个 URL 重写规则配置如下:

<rule name="Reroute themes">
    <match url="^themes.*" />
    <action type="Rewrite" url="/legacy/{R:0}" />
</rule>

这在导航到 URL 时可以正常工作。但是当使用Server.MapPath() 时,它并没有应用重写规则。

Server.MapPath() 真的应该考虑到这一点吗?如果没有,我应该如何在不修改代码的情况下重新路由应用程序?

【问题讨论】:

    标签: iis asp-classic url-rewriting server.mappath


    【解决方案1】:

    我一直在寻找同样的东西,所以我在一个测试应用程序中试了一下。 Server.MapPath() 似乎 不承认 URL 重写模块规则。

    这是我使用空 Web 项目(Razor 语法)进行测试的方法:

    重写规则:

    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Rewrite rule1 for test1">
                    <match url="^test1(.*)" />
                    <action type="Rewrite" url="{R:1}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
    

    cshtml:

    <p>
        The URL for this page is @Request.Url.AbsoluteUri .
        <br />
        MapPath of /test1 is @Server.MapPath("~/test1")
        <br />
        MapPath of / is @Server.MapPath("~/")
    </p>
    

    我点击http://localhost/,然后点击http://localhost/test1。结果是:

    The URL for this page is http://localhost/ .
    MapPath of /test1 is c:\src\temp\test1
    MapPath of / is c:\src\temp\
    

    所以看起来 mappath 基本上是采用System.AppDomain.CurrentDomain.BaseDirectory(或类似的东西)并将其与相对 URL 结合起来。附带说明一下,我已经单独确认 MapPath() 考虑了 1 级虚拟目录,但没有考虑到第 2 级(即 virt 指向另一个也定义了 virt 的位置)。

    【讨论】:

    • 感谢您的确认。你有没有想出一个可以回答我问题第二部分的解决方案?
    【解决方案2】:

    我刚遇到这个问题,现在我要创建一个与我的重写规则相对应的特殊 MapPath 变体。

    所以要么是这样的:

    string MapTheme(string themeName)
    {
        return Path.Combine(Server.MapPath("/legacy"), themeName)
    }
    

    或者,如果您愿意:

    string MapThemePath(string themeUrl)
    {
        Match m = Regex.Match("^themes/(.*)");
        if (!m.Success)
            throw new ArgumentException();
        string themeName = m.Groups[1].Value;
        return Path.Combine(Server.MapPath("/legacy"), themeName)
    }
    

    或概括:

    string MyMapPath(string url)
    {
        Match m = Regex.Match("^themes/(.*)");
        if (m.Success)
        {
            string themeName = m.Groups[1].Value;
            return Path.Combine(Server.MapPath("/legacy"), themeName)
        }
        else if (itsAnotherSpecialRewriteCase)
        {
            return doSomeSimilarTransformation();
        }
        // ...
        else
        {
            // Handle non-rewritten URLs
            return Server.MapPath(url);
        }
    }
    

    我不是特别喜欢这个,因为它违反了“不要重复自己”。

    【讨论】:

    • 我们最终做了类似的事情,将所有 MapPath 路由到执行必要重定向的包装器。
    猜你喜欢
    • 2013-01-01
    • 2023-04-03
    • 2011-12-04
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多