在IIS重写规则中处理可选URL参数的动态数量

浮点数

我的情况如下:场所可以是多个类别的一部分,用户还可以在多个类别类型上添加过滤器,因此我的URL现在如下所示:

  1. /venues/beaches/boats/themeparks (这将显示海滩,船只和主题公园的所有场地)
  2. /venues/beaches/boats
  3. /venues ETC。

因此,不同场所类型(海滩,船只,主题公园等)的数量既是动态的,也是可选的。如何设置我的重写规则,使categoryquerystring参数保留所有不同的场所类型,或者最好将category参数添加多次,并且如果未提供场所类型,则category只会为空/空?

所以对于URL: /venues/beaches/boats/themeparks

我会得到这个重写的URL: search.aspx?category=beaches&category=boats&category=themeparks

对于URL: /venues

我得到这个重写URL: search.aspx?category=或太细会是:search.aspx

我现在有这个:

<rule name="venue types">
  <match url="^venues/([a-zA-Z0-9-+']+)$"/>
  <action type="Rewrite" url="search.aspx?category={R:1}"/>
</rule>

更新

我接受了@Arindam Nayak的以下建议。

我安装了https://www.nuget.org/packages/Microsoft.AspNet.FriendlyUrls.Core/RouteConfig.vbApp_Start文件夹中手动创建了一个文件我添加了(作为测试):

Public NotInheritable Class RouteConfig
    Private Sub New()
    End Sub
    Public Shared Sub RegisterRoutes(routes As RouteCollection)
        Dim settings = New FriendlyUrlSettings()
        settings.AutoRedirectMode = RedirectMode.Permanent
        routes.EnableFriendlyUrls(settings)

        routes.MapPageRoute("", "test", "~/contact.aspx")

    End Sub
End Class

global.aspx.vb我补充说:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RouteConfig.RegisterRoutes(RouteTable.Routes)
End Sub

当我转到时www.example.com/test,它现在可以正确地将我重定向到contact.aspx。(请注意:URL重写-因此规则web.configrewriteRules.config文件-优先于RouteConfig.vb!)

我快到了,因此www.example.com/test可以正确地重定向到file,contact.aspx但这www.example.com/test/boats/outside/more/fields/are/here会引发404错误:

在此处输入图片说明

更新2

我添加了日志到routeConfig.vb

    GlobalFunctions.Log("1")
    Dim settings = New FriendlyUrlSettings()
    GlobalFunctions.Log("2")
    settings.AutoRedirectMode = RedirectMode.Permanent
    GlobalFunctions.Log("3")
    routes.EnableFriendlyUrls(settings)
    GlobalFunctions.Log("4")
    routes.MapPageRoute("", "test", "~/contact.aspx")
    GlobalFunctions.Log("5")

And these lines are all executed. Now something strange is happening: routeConfig.vb seems to only be executed once. So:

  1. I build my app.
  2. I goto /test URL
  3. the lines are logged and I arrive on the contact.aspx page
  4. I refresh the /test page, NO lines are logged

And I also tried:

  1. I build my app.
  2. I goto /test/boats/outside/more/fields/are/here URL
  3. the lines are logged
  4. I get the aforementioned 404 error
  5. I refresh the page, nothing is logged anymore

So it seems routeConfig is only ever hit once (at applicationstart?) and then never hit again. And for request /test/boats/outside/more/fields/are/here it arrives at routeConfig.vb file, but does not show contact.aspx for some reason...

update 3

I found that when I explicitly define the routes, it does work, like so:

routes.MapPageRoute("", "test", "~/contact.aspx")
routes.MapPageRoute("", "test/123", "~/contact.aspx")

So now URL /test and /test/123 work, but that is not dynamic at all as I just want to match on /test and then get the FriendlyUrlSegments.

I can't post the code online, so if it helps, here's my solution explorer: 在此处输入图片说明

What can I do?

Arindam Nayak

To have such kind of SEO friendly URL you need to install “Microsoft.AspNet.FriendlyUrls” nuget package.

Open package manager console – Help. Then type following –

Install-Package Microsoft.AspNet.FriendlyUrls

Then it will automatically add following in RouteConfig.cs.

public static class RouteConfig
{
   public static void RegisterRoutes(RouteCollection routes)
   {
      var settings = new FriendlyUrlSettings();
      settings.AutoRedirectMode = RedirectMode.Permanent;
      routes.EnableFriendlyUrls(settings); 
    }
}

For you case, you need to add following to RouteConfig.cs.

routes.MapPageRoute("", "venues", "~/venues.aspx");

So when you hit url http://www.example.com/venues/beaches/boats/themeparks or http://www.example.com/venues/beaches, it will hit venues.aspx. In venues.aspx.cs, page_load event you need to have following code.

IList<String> str = Request.GetFriendlyUrlSegments();

For case-1, str will be ['beaches','boats','themeparks'] and for case-2 it will be ['beaches'].

For more info you can refer to my blog or similar SO answer here - Reroute query string using friendlyUrl

让我知道,如果您遇到任何问题或问题仍未解决。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

IIS URL重写规则

来自分类Dev

URL重写可选参数

来自分类Dev

参数的url重写规则

来自分类Dev

IIS重写URL规则-OR条件

来自分类Dev

htaccess mod重写规则-可选参数

来自分类Dev

用于可选查询参数的Apache重写规则

来自分类Dev

使用可选参数重写URL

来自分类Dev

动态 URL 的 NGINX 重写规则

来自分类Dev

处理 url 中的可选 urlparse 参数

来自分类Dev

htaccess 重写规则 - url 参数

来自分类Dev

基于长度参数的IIS重写规则

来自分类Dev

htaccess重写规则以从URL中删除参数

来自分类Dev

https的IIS URL重写规则并包含路径

来自分类Dev

IIS URL重写https规则忽略localhost

来自分类Dev

IIS URL重写规则使用条件

来自分类Dev

aspx的IIS URL重写模块规则

来自分类Dev

IIS URL重写规则使用条件

来自分类Dev

IIS 重写规则以删除部分 url

来自分类Dev

重写规则以动态计算参数

来自分类Dev

htaccess mod重写规则,附加url参数

来自分类Dev

IIS中的WebConfig URL重写

来自分类Dev

当文件名加扩展名在 url 中时,不会触发 IIS 重写规则

来自分类Dev

处理QueryDSL中的可选参数

来自分类Dev

Mod重写规则以重写整个站点的URL参数

来自分类Dev

出站IIS重写规则未添加参数,我缺少什么?

来自分类Dev

替换Apache重写规则中的缺失参数

来自分类Dev

重写查询字符串中的URL空值-双斜杠,可选参数

来自分类Dev

我不知道如何编写包含原始url中的参数的重写规则(在我的.htaccess中)

来自分类Dev

htaccess 中的动态 URL 重写