c# - Web.config iis url重写

标签 c# asp.net visual-studio iis

我正在尝试添加一个 iis url 重写规则,该规则在开发环境(在我的本地计算机上)中有所不同,并且一旦上传到服务器并发布(部署)。

我的 IIS 重写规则有效,但我不想记住更改 localhost部署网站后到我的服务器托管地址。有什么建议吗?

下面是我在 <system.webServer> 中的 IIS URL 重写规则

<!-- IIS Rules Rewrite -->
<rewrite>
  <rules>  
<!-- Serve site map with proper XML content type response header. -->
    <rule name="Sitemap XML" enabled="true" stopProcessing="true">
      <match url="sitemap.xml" />
      <action type="Rewrite" url="sitemap.aspx" appendQueryString="false"/>
    </rule>
    <!-- Access block rule - is used to block all requests made to a Web site if those requests do not have the host header set. This type of rule is useful when you want to prevent hacking attempts that are made by issuing HTTP requests against the IP address of the server instead of using the host name -->
    <rule name="Fail bad requests">
      <match url=".*"/>
      <conditions>
        <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
      </conditions>
      <action type="AbortRequest" />
    </rule>
    <!-- HTTP to HTTPS Rule 
    <rule name="Redirect to https" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" negate="false" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
    </rule>-->

最佳答案

我认为您可以使用配置文件转换 (msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx)

注意:整个 system.webServer 部分将替换为 web.release.config

中的内容

web.config

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <rule name="Sitemap XML" enabled="true" stopProcessing="true">
              <match url="sitemap.xml" />
              <action type="Rewrite" url="sitemap.aspx" appendQueryString="false"/>
            </rule>
            <rule name="Fail bad requests">
              <match url=".*"/>
              <conditions>
                <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
              </conditions>
              <action type="AbortRequest" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>

web.release.config

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer xdt:Transform="Replace">
        <rewrite>
          <rules>
            <rule name="Sitemap XML" enabled="true" stopProcessing="true">
              <match url="sitemap.xml" />
              <action type="Rewrite" url="sitemap.aspx" appendQueryString="false"/>
            </rule>
            <rule name="Fail bad requests">
              <match url=".*"/>
              <conditions>
                <add input="{HTTP_HOST}" pattern="YOUR_NEW_SERVER_URL_HERE" negate="true" />
              </conditions>
              <action type="AbortRequest" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
</configuration>

关于c# - Web.config iis url重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108757/

相关文章:

C#声明空字符串数组

c# - 将 lambda 作为参数分配给通过反射调用的泛型方法

c# - Linq Lookup 解析 CSV 文本行

c# - SQLite:插入标识列

css - Visual Studio 在 CSS 文件的开头插入不需要的文本

c# - 为什么 asp.net 使用 GUID 列而不是 bigint

asp.net - ASP.Net-App_Data和App_Code文件夹?

c# - MVCGrid.NET - 如何重新加载网格?

c++ - visual studio 2013如何链接opencv等dll文件输出exe

visual-studio - Visual Studio 编辑器,深入 MEF