Question

I have an application URL that can be launched from browser with query string passed in.

My Development URL is

http://localhost:15094/MyPage.html?user=username&role=admin

Client URL will be path to MyPage.html in the hard Drive

file:///C:/Program%20Files/Client/MyPage.html?user=username&role=admin

When the url is localhost with http, i can extract the query string using

System.Windows.Browser.HtmlPage.Document.DocumentUri.Query
// this gives me ?user=username&role=admin 
// from http://localhost:15094/MyPage.html?user=username&role=admin

But I want ?user=username&role=admin when client use the URL

file:///C:/Program%20Files/Client/MyPage.html?user=username&role=admin

System.Windows.Browser.HtmlPage.Document.DocumentUri.Query does not work with it I suppose.

Please note Development URL is with http and when the application is installed on a Client's machine, the url will be without http, this is how application should work. Please do not suggest to host it in IIS etc.

My Question is very simple:

How would you extract the query string from "file:///C:/Program%20Files/Client/MyPage.html?user=username&role=admin" ? If System.Windows.Browser.HtmlPage.Document.DocumentUri.Query works fine for url's without http, why I am not getting query string right?

Was it helpful?

Solution

A simple way to do this without being clever is to use IndexOf eg:

var originalUrl = "file:///C:/Program%20Files/Client/MyPage.html?user=username&role=admin";
var extractedQueryString = string.Empty;
if(originalUrl.IndexOf("?") != -1)
{
   extractedQueryString = originalUrl.Substring(originalUrl.IndexOf("?"));
}

Wrote this off the top of my head without compiling but think I got it right.

Also to get the filename part of the string if you're wondering would be:

var extractedFileName = originalUrl.Substring(0, originalUrl.IndexOf("?"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top