2

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:

  1. /venues/beaches/boats/themeparks (this will display all venues that are beaches AND boats AND themeparks)
  2. /venues/beaches/boats
  3. /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:

enter image description here

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:

  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: enter image description here

What can I do?

Adam
  • 6,041
  • 36
  • 120
  • 208
  • You can follow my blog on this - https://arindamnayak1.wordpress.com/2015/03/07/seo-friendly-url-routing-in-asp-net/, you can use `Request.GetFriendlyURLSegments`, it will return you array of values, i.e. for `venues/X/Y/Z` it will be `[x,y,z]`, if you need further details, I can post this as an answer. – Arindam Nayak Nov 29 '15 at 09:29
  • @ArindamNayak thanks. Yes, the recommended way of answering on SO is always to answer on SO itself (the external site might be taken down in the future). So yes, I'd love to have your answer here, applied to my scenario :) Thanks again! – Adam Nov 29 '15 at 16:36
  • I have posted my suggestion as answer. – Arindam Nayak Nov 30 '15 at 05:16
  • can you try removing `rewriteRules.config` and then try. – Arindam Nayak Dec 03 '15 at 16:51

1 Answers1

1

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

Let me know,if you face any issue or still your issue is unresolved.

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • I'm going through the NuGet Package manager in Visual Studio and see `Microsoft.AspNet.FriendlyUrls`, but also `Microsoft.AspNet.FriendlyUrls.Core` where the description of the latter seems to match my needs more. Which one do I need? And I'm using VB.NET and don't have a RouteConfig file yet...does that matter? – Adam Dec 01 '15 at 14:22
  • I would suggest to go with `Microsoft.AspNet.FriendlyUrls`, if that fails then go with 2nd one. You can go for a simple MVC based project ( a side project), if that works there, then take the components and make it work with existing one. – Arindam Nayak Dec 02 '15 at 05:36
  • Thanks, I'm almost there I think, one final error seems to throw me off...see my updated post. Hope you can help! (ps my apologies for the many post updates) – Adam Dec 02 '15 at 22:20
  • @Flo, it should work, can you share your project in github or zip file? – Arindam Nayak Dec 03 '15 at 16:42
  • unfortunately I can't do that...I removed the rewriteRules.config completely now, but still the error persists...I added `update 2` section to my original post...do you have any other debug suggestions? Thanks again! – Adam Dec 03 '15 at 22:13
  • @Flo, since the code is written in `app_start` , it is executed once. Are you trying to host the application and try or while debugging ( like `localhost:XXXX`), it is not working? – Arindam Nayak Dec 04 '15 at 05:48
  • thanks. I'm running the app on my local machine using the full hostname, so `www.example.com`, I'll try to run it on localhost too, although I don't see (yet) how that would help :) – Adam Dec 04 '15 at 14:15
  • @Flo, just because to rule out any server specific setting. – Arindam Nayak Dec 04 '15 at 19:19
  • see my update 3....it does work when explicitly defining the paths...why is it not working on just the url part I define and thus more dynamically? (ps I upvoted your answer, but will assign reward when this last part is solved :)) – Adam Dec 06 '15 at 17:01
  • @Flo, give me some time, I'll check and will let you know. – Arindam Nayak Dec 07 '15 at 06:13