River
River

Reputation: 1487

ASP.NET Parse URL Info

Aside from writing custom code are there any existing features in .NET which allows me to parse information from part of a URL? So my input will be like the below:

\Shop-Pet\Dog\Large\Africa-Lionhound\27-80?color-Dark

Is there a any easy way instead of using string split and/or regex afterwards to extract information into an array like the below:

[shop,pet]
[PetType, Dog]
[BreedSize, Large]
[Country, Africa]
[OtherNames, Lionhound]
[Height, 27]
[Weight, 80]

Thanks

Upvotes: 1

Views: 545

Answers (1)

Doozer Blake
Doozer Blake

Reputation: 7797

While not exactly what you need, Request.Url.Segments will return a string array of the different segments available to you. Given your example: /Shop-Pet/Dog/Large/Africa-Lionhound/27-80?color-Dark

it would return something to you like

{string[6]}
    [0]: "/"
    [1]: "Shop-Pet/"
    [2]: "Dog/"
    [3]: "Large/"
    [4]: "Africa-Lionhound/"
    [5]: "27-80"

You would have to handle the QueryString separately.

I don't think there's anything specifically like what you want, but Routes which are available in ASP.NET 4.0 could probably be setup to handle things exactly like you wanted.

Upvotes: 1

Related Questions