user2061217
user2061217

Reputation:

Friendly URLs and Query Strings

In my project (ASP.NET Web Forms) I want to use Friendly URLs, installed from NuGet.

I registered route in global.asax file:

Public Shared Sub RegisterRoutes(routes As RouteCollection)  
    routes.MapPageRoute("Route", "default/{id}", "~/default.aspx?id={id}")
End Sub

With this code, I can use default/123 instead of default?id=123. I want to add name, assigned to the id, in the url. So I can have url like this: default?123-Firstname-Lastnam. Name is saved in database, in single column. How can I add second parameter (name) to the url, add symbol - and display it without letters like this: řčš (because the application is in Chech language. Thanks for answer.

Upvotes: 2

Views: 4753

Answers (1)

MikeSmithDev
MikeSmithDev

Reputation: 15797

To use FriendlyUrls, after you install it from NuGet, go to your global.asax and enable it:

Imports Microsoft.AspNet.FriendlyUrls

Public Class Global_asax
    Inherits System.Web.HttpApplication

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

    Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.EnableFriendlyUrls()
    End Sub
    'rest of global

That is pretty much it. To get the values out of a URL for a page, you'll need to loop through the URL segments (don't forget Imports Microsoft.AspNet.FriendlyUrls):

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each segment As String In HttpRequestExtensions.GetFriendlyUrlSegments(Request)
        Dim val As String = segment
    Next
End Sub

So visiting siteURL.com/default/123 will loop once and give you 123, while siteURL.com/default/122/Bilbo/Baggins will loop three times and give you 122, Bilbo, and Baggins.



Or, if you just want to use plain routing and not FriendlyUrls:

routes.MapPageRoute("id-route", "default/{id}", "~/default.aspx")

One good thing about routing is you can use the URL to pass variable data without using query strings. So the route to pass name data could look like

Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.MapPageRoute("name-route", "default/{id}/{firstName}/{lastName}", "~/default.aspx")
End Sub

And then default.aspx could be hit with siteURL.com/default/123/Frodo/Baggins and has:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim id As Integer = 0
    Int32.TryParse(Page.RouteData.Values("id"), id)
    Dim firstName As String = Convert.ToString(Page.RouteData.Values("firstName"))
    Dim lastName As String = Convert.ToString(Page.RouteData.Values("lastName"))
    'do something if id > 0
End Sub



Other Considerations: If you only want name in a single column, then you can combine the firstName and lastName variables for saving. Using - as a delimeter like you show in question isn't a good idea, as people can have hyphenated names. Saving name in a single column tends to cause problems as it makes it much harder to sort by first or last name, etc.

Also it appears you will be inserting into your database from a GET command. I would think this would be much more clear to do using PUT or POST.

Upvotes: 5

Related Questions