【问题标题】:Alternate way to "<rewrite>" web.config. How to accomplish it programatically. WCF\"<rewrite>\" web.config 的替代方法。如何以编程方式完成它。 WCF
【发布时间】:2022-11-19 13:59:50
【问题描述】:

我的 WCF 有以下两条规则:

<system.webServer>
    <rewrite>
      <rules>
        <!-- Rewrite requests to /MyService to /MyService.svc -->
        <rule name="MyService" stopProcessing="false">
          <match url="MyService/(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="/MyService.svc/{R:1}" />
        </rule>
        <!-- Remove path /MoreServices from url -->
        <rule name="example" stopProcessing="true">
          <match url=".*(MoreServices)(.+)" ignoreCase="true" />
          <action type="Rewrite" url="{R:2}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

第一条规则:

<!-- Rewrite requests to /MyService to /MyService.svc -->

<rule name="MyService" stopProcessing="false">
    <match url="MyService/(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
    </conditions>
    <action type="Rewrite" url="/MyService.svc/{R:1}" />
</rule>

将在没有 svc 扩展的情况下发送的调用重写到 svc 服务。

第二条规则:

<!-- Remove path /MoreServices from url -->
<rule name="example" stopProcessing="true">
    <match url=".*(MoreServices)(.+)" ignoreCase="true" />
    <action type="Rewrite" url="{R:2}" />
</rule>

删除额外的路径并定向到正确的服务。

一切正常,但是,我将发布到的站点不允许在 web.config 中使用 &lt;rule&gt; 标记。我的问题是,如何修改我的 WCF 以编程方式完成上述两条规则。基本上在 C# 中复制上述两项的逻辑作为 WCF 服务的一部分。谢谢你。

【问题讨论】:

    标签: c# asp.net wcf wcf-binding


    【解决方案1】:

    我找到了解决方案!

    非常容易修复,首先从您的 web.config 中删除整个 &lt;rewrite&gt; 部分。如果您还没有新文件(类型 global.asax),那么只需将它添加到您的项目中。

    在 Application_BeginRequest 部分添加以下代码:

    protected void Application_BeginRequest(object sender, EventArgs e)
        {
    
            // Raw URL
            HttpContext context = HttpContext.Current;
            string rawUrl = context.Request.RawUrl;
    
            // Extra Path to be removed
            const string EXTRA_PATH = "/MoreServices";
    
            // Rewrite requests to /MyService to /MyService.svc
            if (!rawUrl.Contains(".svc"))
            {
                // Index of last forward slash
                int idxLastSlash = rawUrl.LastIndexOf('/');
                // Insert .svc before last slash
                rawUrl = rawUrl.Insert(idxLastSlash, ".svc");              
            }
    
            // Remove extra path EXTRA_PATH from url
            if (rawUrl.Contains(EXTRA_PATH))
            {
                // Remove extra path
                rawUrl = rawUrl.Replace(EXTRA_PATH, "");
    
            }
    
            // Rewrite Path if it has been modified
            if (rawUrl != context.Request.RawUrl)
            {
                context.RewritePath(rawUrl, false);
            }
    
        }
    

    这与 web.config 中的上述两条规则的工作方式相同。希望这会对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多