在ASP.Net 4.5 Webforms中使用子域路由和可选参数重写IIS URL

在ASP.Net 4.5 Webforms中使用子域路由和可选参数重写IIS URL,asp.net,regex,iis,url-rewriting,url-routing,Asp.net,Regex,Iis,Url Rewriting,Url Routing,我正在尝试将IIS URL重写与ASP.NET4.5WebForms站点中的子域和子目录路由结合起来 我正在尝试将IIS中的URL重写与ASP.NET4.5中的Web表单站点路由结合起来。我现在的解决方案非常适合用可选参数将子域重写为路由值,但我很难将子目录URL传递给路由或查询字符串。我正在使用的大多数参数也是可选的,所以规则必须考虑这些参数的任意组合或无。p> 最终目标是在子域端使用连字符分隔的位置,在子目录端使用类别,从而实现羽化效果。例如,包含所有参数的URL类似于 我在web.conf

我正在尝试将IIS URL重写与ASP.NET4.5WebForms站点中的子域和子目录路由结合起来

我正在尝试将IIS中的URL重写与ASP.NET4.5中的Web表单站点路由结合起来。我现在的解决方案非常适合用可选参数将子域重写为路由值,但我很难将子目录URL传递给路由或查询字符串。我正在使用的大多数参数也是可选的,所以规则必须考虑这些参数的任意组合或无。p> 最终目标是在子域端使用连字符分隔的位置,在子目录端使用类别,从而实现羽化效果。例如,包含所有参数的URL类似于

我在web.config中为子域和RouteConfig.cs中的路由制定的规则如下:

<rewrite>
      <rules>
      <rule name="Rewrite subdomains">
        <match url=".*" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="^(^$|[^\-]*)(^$|[\-]?)([^\-]\w)\.example\.net$" />
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Rewrite" url="{ToLower:/local/{C:3}/{C:1}}" />
    </rule>

      </rules>
      <outboundRules>
        <rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1">
          <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
          <action type="Rewrite" value="http://example.net/{R:1}" />
        </rule>
        <preConditions>
          <preCondition name="ResponseIsHtml1">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>


From RouteConfig.cs

// Geographic SubDomains
            routes.MapPageRoute("local/state", "local/{state}", "~/local/Default.aspx");
            routes.MapPageRoute("local/state/city", "local/{state}/{city}", "~/local/Default.aspx");


Potential Conflicting routes:

//Category
            routes.MapPageRoute("category", "{catslug}", "~/category/Default.aspx");
            //SubCategory
            routes.MapPageRoute("category/subcategory", "{catslug}/{scatslug}", "~/category/Default.aspx");

到~/local/{state}/{city}/{categoryslug}/{subcategoryslug}但是我看到该路由与类似~/local/{state}/{categoryslug}的路由之间存在潜在冲突,我认为应用程序无法区分state/category和state/city

我还有一些类别/子类别的潜在冲突路由,根域使用slug显示类别。我担心的是,包含相同类别/子类别模式的路由可能最终被发送到根域而不是子域

出于上述考虑,我认为对类别使用querystring比使用routes更好。现在我可以加载一个页面在刚刚好。接下来,我希望能够将这些查询字符串更改为categoryslug/subcategoryslug,并将其重写为/local/{state}/{city}/?categoryslug=categoryslug&subcategoryslug=subcategoryslug。我不想将url重写为类似于?category=&subcategory=的内容,因为这与页面基于查询字符串选择数据的方式不兼容。



您能详细解释一下您的要求吗?我不明白你到底想做什么。我稍微改变了一下,路由现在是{state}-{city},模式现在以\.net(.*)结束,重写为条件4。我已经在没有重写规则的情况下测试了路由,它工作得很好。问题似乎在于重写规则没有通过第四个条件。谢谢你分享你的经验。如果您能发布解决方案并将自己标记为答案,我们将不胜感激。因此,你的帖子可以帮助更多的人。发布的答案,应该对任何试图路由通配符子域和路径信息的人都有用,这是我应该一直拥有的,而不是R:1
  <rule name="Rewrite subdomains">
    <match url=".*" />
    <conditions>
      <add input="{HTTP_HOST}" pattern="^(^$|[^\-]*)(^$|[\-]?)([^\-]\w)\.example\.com$" />
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="{ToLower:/local/{C:3}{C:2}{C:1}{PATH_INFO}}" appendQueryString="true" />
</rule>

  </rules>
  <outboundRules>
    <rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1">
      <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^/(.*)" negate="false" />
      <action type="Rewrite" value="http://example.com/{R:1}" />
    </rule>
    <preConditions>
      <preCondition name="ResponseIsHtml1">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
      </preCondition>
    </preConditions>
  </outboundRules>
</rewrite>