dudledok
dudledok

Reputation: 2880

How to query the current URL to see if it contains a certain string

What I want to do is take the full contents of the address bar and see if everything after the site path (i.e. everything after the question mark), contains a certain string, and if it does then return a path using the site URL and that string.

An example and some code to make things understandable:

If I'm on www.example.com/?blah=image I want the code to look for the string image (after the question mark to prevent sites with image in the name to screw things up) and if it exists return www.example.com/images/ (with that current domain being looked up and not manually written as this will be used on mutliple sites)

Below is what I have written so far.

Public ReadOnly Property StorageRoot() As String
    Get
        Dim currentabsolute As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
        Dim currentdomain As String = CurrentDomain
        If currentabsolute.Contains("Media") Then
            Return System.AppDomain.CurrentDomain.BaseDirectory & "\media\"
        ElseIf currentabsolute.Contains("Docs") Then
            Return System.AppDomain.CurrentDomain.BaseDirectory & "\docs\"
        ElseIf currentabsolute.Contains("Image") Then
            Return System.AppDomain.CurrentDomain.BaseDirectory & "\images\"
        End If
    End Get
End Property

I know CurrentDomain won't return anything and System.Web.HttpContext.Current.Request.Url.AbsolutePath doesn't seem to return what I am looking for so those are part of what I am hoping to get help with.

Any help will be appreciated and if you need any more clarification just ask.

Edit: Updated code

Upvotes: 1

Views: 6677

Answers (2)

JamieSee
JamieSee

Reputation: 13030

Try this and let me know how it does for you:

Imports System.Web

...

Public ReadOnly Property StorageRoot() As String 
    Get
        Dim requestUrl As Uri = HttpContext.Current.Request.Url
        Dim newUrl As New UriBuilder(requestUrl.Scheme, requestUrl.Host, requestUrl.Port, HttpContext.Current.Request.ApplicationPath)
        Dim currentQuery As String = requestUrl.Query 

        If String.IsNullOrEmpty(currentQuery)
            ' What to do if there is no query string?
        Else If currentQuery.Contains("Media", StringComparer.InvariantCultureIgnoreCase) Then 
            newUrl.Path = newUrl.Path + "/media/" 
        ElseIf currentQuery.Contains("Docs", StringComparer.InvariantCultureIgnoreCase) Then 
            newUrl.Path = newUrl.Path + "/docs/" 
        ElseIf currentQuery.Contains("Image", StringComparer.InvariantCultureIgnoreCase) Then 
            newUrl.Path = newUrl.Path + "/images/" 
        End If 

        Return newUrl.ToString()
    End Get 
End Property 

Upvotes: 1

lsuarez
lsuarez

Reputation: 4992

You're looking for the Request.QueryString member. To read the value of a specific query string parameter, try something along the lines of:

If Request.QueryString("Media") IsNot Nothing Then
    Return currentdomain + "/media/"
ElseIf ...

If a query string does not contain the key in question, accessing that index will return nothing; otherwise it will return the value of that parameter. For instance, you could test to see if the string was constructed as ?Media=MyMedia with If Request.QueryString("Media") = "MyMedia".

If you want the raw query string itself, you could parse the Request.RawUrl member for everything after the question mark with something like:

Dim queryString As String = Request.RawUrl.SubString(Request.RawUrl.IndexOf("?"c) + 1)

Upvotes: 3

Related Questions