【问题标题】:IIS URL Rewriting with Routing for Subdomains and Optional Parameters in ASP.Net 4.5 Webforms使用 ASP.Net 4.5 Webforms 中的子域和可选参数的路由重写 IIS URL
【发布时间】:2020-02-14 20:57:19
【问题描述】:

我正在尝试将 IIS URL 重写与 ASP.Net 4.5 网络表单站点中的子域和子目录路由结合起来。

我正在尝试将 IIS 中的 URL 重写与 ASP.Net 4.5 中的路由结合起来用于 Web 表单站点。我现在拥有的解决方案非常适合使用可选参数重写子域来路由值,但我正在努力将子目录 URL 传递给路由或查询字符串。我正在使用的大多数参数也是可选的,因此规则必须考虑这些参数的任何组合,或者什么都不考虑。

最终目标是羽化效果,在子域侧使用连字符分隔的位置,在子目录侧使用类别。例如,包含所有参数的 URL 类似于 http://city-state.example.com/categoryslug/subcategoryslug

我在 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");

最终的预期结果应该将 http://city-state.example.com/categoryslug/subcategoryslug 之类的内容重写为 ~/local/{state}/{city}/{categoryslug}/{subcategoryslug} 但我发现该路线与 ~/ 之类的内容之间存在潜在冲突local/{state}/{categoryslug} 我认为应用程序无法区分 state/category 和 state/city。

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

由于上述问题,我认为对类别使用查询字符串会比使用路由更好。现在我可以在http://city-state.example.com/?category=categoryslug&subcategory=subcategoryslug 加载一个页面就好了。接下来,我希望能够将这些查询字符串更改为 categoryslug/subcategoryslug 并将其重写为 /local/{state}/{city}/?categoryslug=categoryslug&subcategoryslug=subcategoryslug。我不希望将 url 重写为 ?category=&subcategory= 之类的内容,因为这与页面根据查询字符串选择数据的方式不兼容。

【问题讨论】:

  • 你能详细解释一下你的requiremnet吗?我不明白你到底想做什么。
  • 好吧,我已经稍微改变了路由现在是 {state}-{city} 并且模式现在以 \.net(.*) 结束,重写为条件 4。我已经测试了没有重写规则,它工作正常。似乎问题在于重写规则没有通过第四个条件。那是 \.net 之后的任何东西
  • 感谢您分享您的经验。如果您可以发布解决方案并将自己标记为答案,我们将不胜感激。让您的帖子可以帮助更多人。
  • 发布了答案,应该对任何尝试路由通配符子域和路径信息的人有用,这是我应该一直拥有的,而不是 R:1

标签: asp.net regex iis url-rewriting url-routing


【解决方案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>

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多