How to control colon (:) URL Query String? - c#

I need to pass the Query String of ID in which colon (:) is included i.e. ABC_PD:123456.
When I am using this ID in query String session and when its redirect to another page in URL it give 404 no error found error on webpage.
So can any one provide the solution for this so that I can pass the colon in query string and when Page will be redirect without 404 error.
Solution would be much appreciated.

When you build the URL that you redirect to, you need to encode special characters by using the UrlEncode-method:
var redirectTo = "/mypage.aspx?id=" + HttpUtility.UrlEncode("id123:456");
This will create a query string that looks like this and will be interpreted correctly:
"/mypage.aspx?id=id123%3A456"

Related

Problems with aspx getting url with parentheses

I am getting problems with ASP getting the URL when it has parentheses.
My site has URLs like this one: http://example.com/my-amazing-product-%28online%29
So, I am trying to get that URL using the instruction:
var requestURL = HttpContext.Current.Request.RawUrl;
But, that instruction is getting the URL: http://example.com/my-amazing-product-(online)
It automatically converts the parentheses from %28 to ( and from %29 to ), but I don't need that conversion, I need the original URL with %28 and %29
And I can't use replace to fix the URL. I have to get that URL directly from the request or something similar.
Can anyone explain that weird behavior and solve the problem?

How to pass url with query string as an api parameter?

I am curently integrating the api to my application. One of the parameter is content_url which is a valid HTML URL for content. How to I pass whole url with query string?
API Endpoint parameter is located below.
var contentUrl
= "https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml/OSMANLIKP/FXBULTEN/2020-01-20/null";
var url = "https://campaigns.zoho.com/api/v1.1/createCampaign?" +
"resfmt=json&" +
"campaignname=Campaign12&" +
"from_email=mert.metin#finnet.gen.tr&" +
"subject=Test Create Campaign 11&" +
"list_details=%7Bd6ef220cc42b50d7ddf3236cbb9b8847894375f7a7d2d66f%3A%5B%5D%2Cd6ef220cc42b50d71438a8b019c635e5894375f7a7d2d66f%3A%5B%5D%7D&" +
"content_url=" + contentUrl;
Content url is https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP&Kod=FXBULTEN&Tarih=2020-01-20
But I post url value, API gets the url until first & ampersand character. So remain parameter was missing. I need to pass whole url as a parameter.
Expected Result=https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP&Kod=FXBULTEN&Tarih=2020-01-20
Actual Result=https://www.finnet.com.tr/CMS/OSMANLIMENKULTEST/MailSablon/RaporPaylasimHtml?SiteKod=OSMANLIKP
My application is based on MVC and C# language. I also enclosed the url with double quotes but it is not work because of Api Response which is INVALID IMPORT URL.
You must encode the URL before passing as a parameter as
myURL="myURL1?redir=" + Server.URLEncode("myURL2?id=4&user=test");
It will create a correct URL without '?' and "&" characters as
myURL= myURL1?redir=myURL2%3id%34%26user%3dtest
To extract your redirect url from this encoded url you must use
HttpServerUtility.UrlDecode to turn it into a correct url again.

Weird return url

I am trying to use oauth2.
I make a get request, and then I get redirected at a callback url that I have set up before. The problem lies in the fact that the url parameters get preceded by the # sign and thus php, .net can't read them!
I get redirected in the following url in my browser:
http://localhost:1787/About.aspx?#access_token=f3EToovT2bQNNOQ&token_type=bearer&merchant_id=A6BGD4BH&response_type=token
Request.Params is empty, request.query string is empty. Even when I use php and print the $_REQUEST array still is empty!
How is this possible?
Whatever comes after the # doesn't mark as DATA being sent to the server, but a hash on the client side.
Try redirect using JavaScript only the hash:
window.location = window.location.pathname + '?' + window.location.hash.substring(1);

server.htmlencode of asp.net equivalent in asp.net mvc

I have a ASP.NET MVC controller which is making call to another service using HttpClient class.
var url = "some url";
var client = new HttpClient();
var result= client.GetAsync(url);
The URL I am sending contains some special characters. How can encode special characters in ASP.NET MVC controller?
Thanks!!1
Try this:
url = HttpUtility.UrlEncode(url);
As you are considering a URL that will be used as such for a request (with HttpClient.getAsync) -- not as an argument within another URL -- you should use Uri.EscapeUriString.
Here is a comparison of three methods for the following URL:
var url = "http://some url?data=x y+z&user=1#ok";
HttpUtility.UrlEncode
Console.WriteLine(HttpUtility.UrlEncode(url));
http%3a%2f%2fsome+url%3fdata%3dx+y%2bz%26user%3d1%23ok
Obviously, this is not desired: the URL got damaged with / escaped, a + entered in the path, ...etc. The method seems useful for the query part of the URL, but not for the whole lot.
HttpUtility.UrlPathEncode
Console.WriteLine(HttpUtility.UrlPathEncode(url));
http://some%20url?data=x y+z&user=1#ok
This looks useful, although the space is a bit of a problem in the query part (notice the broken hyperlinking here, although browser can deal with it). But more importantly, the method is being deprecated:
Do not use; intended only for browser compatibility. Use UrlEncode.
Uri.EscapeUriString
Console.WriteLine(Uri.EscapeUriString(url));
http://some%20url?data=x%20y+z&user=1#ok
This seems to do the job well: %20 is an escape sequence that all modern browsers should support, also when occurring in the query part of the URL.
There is no need it encoding in Razor View Engine starting from 3rd version, and it's very convenient. Instead if you want to use tags you should use:
#Html.Raw(myString)
So basically just using Razor comes with encoding by default.
You should use HttpUtility.UrlPathEncode
When you use url = HttpUtility.UrlEncode(url) it doesn't work fine with spaces.

HTTP 404 Error after encoding Url

When I use HttpUtility.UrlEncode to encode a Url I end up getting a server error.
ASP.Net code:
NavigateUrl=<%# HttpUtility.UrlEncode(string.Concat("UpdateMember.aspx","?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString())) %>
Url:
http://localhost/UITest/MM/UpdateMember.aspx%3fgroupId%3d0032409901
which results in "HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
However using:
NavigateUrl=<%# string.Concat("UpdateMember.aspx","?groupId=", DataBinder.Eval(Container.DataItem, "GroupID").ToString()) %>
results in the Url:
http://localhost/UITest/MM/UpdateMember.aspx?groupId=0032409901
which works out fine. Am I doing something incorrectly?
You shouldn't encode the entire URL, atleast not the 1st "?" symbol. If you encode the ? too then your application looks for a file with the name & extension "UpdateMember.aspx%3fgroupId%3d0032409901" which doesn't exist.
Probably, this is what you should do.
http://localhost/UITest/MM/UpdateMember.aspx?groupId%3d0032409901
HttpUtility.UrlEncode() URL-encodes a string
That means that it escapes all special characters from the string so that you can insert it as part of a URL without any characters being parsed as URL modifiers.
You use this kind of escape function when inserting arbitary text as part of a URL.

Categories

Resources