我的情况如下:场所可以是多个类别的一部分,用户还可以在多个类别类型上添加过滤器,因此我的URL现在如下所示:
/venues/beaches/boats/themeparks
(这将显示海滩,船只和主题公园的所有场地)/venues/beaches/boats
/venues
ETC。因此,不同场所类型(海滩,船只,主题公园等)的数量既是动态的,也是可选的。如何设置我的重写规则,使category
querystring参数保留所有不同的场所类型,或者最好将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.vb
在App_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.config
或rewriteRules.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:
/test
URLcontact.aspx
pageAnd I also tried:
/test/boats/outside/more/fields/are/here
URLSo 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?
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] 删除。
我来说两句