Ok, I have a string of the form
string temp = "http://www.example.com?file=666111&submitter=Betty&origin=Office&telNo=05555";
what I need to do is extract the value of the file variable in order to use it. If this was the referrer url I could've done Request.QueryString and got it but the problem is that I have it as a string variable.
I could try to do substring and get the value but I was hoping there was a cleaner way to do this?
Perhaps you can use the HttpUtility.ParseQueryString method. It returns a NameValueCollection with all parameters.
Related
Let's say my url is "https://www.mywebsite.com/app/company/employees/5" or "https://www.mywebsite.com/company/employees?id=5&name=jack"
I'm looking for a way to get the "base" path, or whatever it's called. Like the "base" path would be "/app/company/employees" for both, without the "/5" part or "?id=5&name=jack" part.
I was using string.Join("/", request.ApplicationPath, request.RequestContext.RouteData.Values["controller"], request.RequestContext.RouteData.Values["action"]) to get it (request is HttpRequestBase), but it doesn't work the way I want since it includes the Index action too. Like if the Url is "https://www.mywebsite.com/app/company" I want "/app/company/" not "/app/company/Index". I can always check if the action is Index or not but it feels like kind of a "code smell" to me.
Is it even a code smell? Is there any proper way to accomplish this?
The Uri Class offers many properties https://learn.microsoft.com/en-us/dotnet/api/system.uri?view=netframework-4.7.2
Try this
string val = HttpContext.Current.Request.Url.AbsolutePath;
or in your case if your request variable is an HttpRequest object, try:
string val = request.Url.AbsolutePath;
Use HttpContext.Current.Request.Url.AbsolutePath
I am using a simple statement
NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString);
I am trying to grab the values passed in the query string. A simple example of something passed would be
form_id=webform_client_form_4&referrer=http://myURL/webform/request-information?agent=agent&keyword=keyword&full_name=Jon Harding&company=RTS Financial - TEST&phone=913-555-5555
The issue I am having is that the referrer is actually the full string after it. I do need the other values. In this case becauase the ParseQueryString splits the string at the & character the first value is always ignored in the NameValueCollection
Currently when I try to get the value of the agent it is a blank value, understandably.
What would be the best method to make sure I grab all variables? I could split the string at the question mark and then prepend an ampersand to the string before I do the ParseQueryString. Is there a more elegant want to do this?
This will work with least effort:
NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString.Replace("?","&"));
In the code Uri.TryCreate(link, UriKind.Absolute, out testurl) the link is tested if valid and gives a Uri object for it. Should I then use original string link or string testurl.AbsoluteUri ? because both seem to have same string value after code execution.
You can use any. But the Uri object can give you more details about the url like Host, Scheme etc.
If there is no difference, it doesn't matter. Basically, you are just using the TryCreate to validate your URL, so using the original string should be just fine.
You should check for the result of TryCreate and use the resulting object if successful, otherwise fail gracefully; if it's purely for validation and the input and output are equivalent (if 'correct') then this is a non-issue.
I am stumped on this scenario. Basically I have an URL that is passed to a aspx page and then I try to get query string from the URL, but what happens is when I try to get the query string from the URL it omits the '+' and replaces it with an whitespace.
My URL = http://localhost:3872/Test.aspx?mt=jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=
Request["mt"] =jan1TNIixxA1 8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet q7rq7FoTJf S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s 0R7vW8I0S9d765RHdYU2xkRuojHYZU=
As you can see these two strings are different.
I thought it was the object to string conversion but this does not seem to be the case cause the value of the object has the omitted '+' before conversion.
What can be done to avoid this character replacement (I want to try and avoid string manipulation)
Also what could the cause of this be?
you are getting that because + is the url encoded representation of space " ". If you want to preseve the plus sign in your value you will need to url encode it:
Send that querystring in URL encoded formate and then you will get the expected result.
see: why Request.QueryString replace + with empty char in some cases?
You can use
MyUrl = MyUrl.Replace("+",#"%2B");
The problem is the '+' character is being converted to whitespace if you use httprequest. If you convert it to its hex value, you can pass it with no problem.
You should use HttpUtility.UrlEncode to generate you parameter value. Currently it seems you are using base64 encoding which is not optimal for query parameters.
Use this:
mt=encodeURIComponent(mt);//if mt be --> jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=
Response.Redirect("Test.aspx?"+mt);
this will encode your URL and after this '+' will converted to "%2B" and if you want to read encoded URL it will not converted to space.
from here
If it is really so important to avoid changing the string when you send it, you could chanhe it AFTER you get ir from httprequest. Maybe you could use:
MyUrl = (Request["mt"].Replace(" ","+"));
There is no possibility to pass the space in url, so when you have a space, you can be sure that there was a '+' in there.
You can get the query string using following method
string strQuery = Request.Url.Query;
I have a situation where I need to put a url inside a GET variable.
for example:
'http://www.site.com/StepOne?b=1&afterUrl=http://www.site.com/StepTwo?someVar=text&b=1'
In this case, when I am at StepOne the b param value will be 1,1 and not 1 as expected.
the afterUrl param will be:
'http://www.site.com/StepTwo?someVar=text'
instead of this:
'http://www.site.com/StepTwo?someVar=text&b=1'
How do I isolate the afterUrl param so its own GET variables won't effect the entire URL.
When you are creating the afterUrl URL parameter, be sure to UrlEncode() the value.
e.g.
var url = String.Format("http://www.site.com/StepOne?b={0}&afterUrl={1}", b, Server.UrlEncode(afterUrl));
Consider using HttpUtility.UrlEncode() for the AfterURL
(EDIT or Server.URLEncode() as others have pointed out)
"http://www.site.com/StepOne?b=1&afterUrl=" +
HttpUtility.UrlEncode(http://www.site.com/StepTwo?someVar=text&b=1");
Then when you finally hit the "StepOne" page you can use HttpUtility.UrlDecode(AfterURL variable name). From there you can Response.redirect or whatever you want with the preserved after url.
Use Server.UrlEncode on someVar to escape out it's querystring values before putting it in the link. You may need to use Server.UrlDecode on the other side to convert it back to the original characters.
Quite simply, you need to URL encode the afterUrl param (actually, you should URL encode all parameters passed to a server), which will turn "http://www.site.com/StepTwo?someVar=text&b=1" into "http%3A%2F%2Fwww.site.com%2FStepTwo%3FsomeVar%3Dtext%26b%3D1", which won't affect the set of parameters. Almost any server framework on the market will automatically decode that back into the string "http://www.site.com/StepTwo?someVar=text&b=1", or at least give you a function to do so yourself.
EDIT:
As this SO question shows, it is possible to URL encode a string without using System.Web, using System.Net.Uri.EscapeDataString().