My scenario is as follows: a venue can be part of multiple categories and users can also add filters on multiple category types, so my URLs now are like:
/venues/beaches/boats/themeparks
(this will display all venues that are beaches AND boats AND themeparks)/venues/beaches/boats
/venues
etc.
So the number of different venue types (beaches, boats, themeparks etc) is both dynamic AND optional. How can I setup my rewrite rules that the category
querystring parameter holds all the different venue types or preferably the category
parameter is added multipe times and if no venuetype is provided category
will just be empty/null?
So for URL: /venues/beaches/boats/themeparks
I'd get this rewritten URL: search.aspx?category=beaches&category=boats&category=themeparks
And for URL: /venues
I'd get this rewritten URL: search.aspx?category=
(or fine too would be: search.aspx
)
I now just have this:
<rule name="venue types">
<match url="^venues/([a-zA-Z0-9-+']+)$"/>
<action type="Rewrite" url="search.aspx?category={R:1}"/>
</rule>
update
I went with the below suggestion of @Arindam Nayak.
I installed https://www.nuget.org/packages/Microsoft.AspNet.FriendlyUrls.Core/ and manually created a RouteConfig.vb
file in App_Start
folder. I added (as a test):
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
And in global.aspx.vb
I added:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
RouteConfig.RegisterRoutes(RouteTable.Routes)
End Sub
When I go to www.example.com/test
, it does now correctly redirect me to contact.aspx. (Be aware: URL Rewriting - so rules in web.config
or rewriteRules.config
file - take precedence over RouteConfig.vb
!)
I'm almost there, so this www.example.com/test
redirects correctly to file contact.aspx
But this www.example.com/test/boats/outside/more/fields/are/here
, throws a 404 error:
update 2
I added logging to 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:
- I build my app.
- I goto
/test
URL - the lines are logged and I arrive on the
contact.aspx
page - I refresh the /test page, NO lines are logged
And I also tried:
- I build my app.
- I goto
/test/boats/outside/more/fields/are/here
URL - the lines are logged
- I get the aforementioned 404 error
- 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?