Web Api 2 routing issue with special characters in URL - c#

In the middle of developing a Web Api 2 REST service, we have discovered a routing problem. The endpoint is as follows:
.../{email}/...
The problem is that the email could contain special characters such as '+', which results in a 404 resource not found.
We would really like for the user of the service, to be able to specify the email in the URL. But since the email also legally can contain an '&', this can't just be moved to an URL parameter. How would we go about solving this?
Regards
Frederik

UrlEncodeUnicode and UrlDecode should be helpfull in your case.

No, encoding and deconding can only work if yyou're in control of the client and server operations. If otherwise, the best way is to call the endpoint as such
www.yourwebsite.com/api/account/create?email='{email with any characters}'

Related

Problem with special character when using c# api

i have a problem with using character when i want to get user information by using email
the problem is special character in the email
because the api link not supports character for example
this is the Route im using in the api
[Route("api/v1/Users/{Email}")]
and this is the link
api/v1/Users/majed email.com
any special character let the GET not even see the request link
how can i solve this problem ?!
In most scenarios it has been found that a 404 is returned if an email address is sent. The issue is usually caused by the "." in the email address.
If you add a trailing slash it should resolve your issue.
api/v1/Users/majed#email.com/
You could use URL encoded format of the string.
The string majed#email.com would encoded be majed%40email.com.
You can read more at W3 Shools URL Encoding Reference.

URL with dot causes 404 error

I have an Asp.Net Web Api application which takes ip address as input and return the country of that respective ip.
when I pass the below url
http://localhost:portno/xx.xx.xx.xx
it throws Error 404
can any one tell me why?
Short hacky answer: Add a trailing forward slash. IIS thinks it's a file at the end of hte URL.
Longer, more elegant answer: Check out these links for cleaner fixes that require more work:
ApiController returns 404 when ID contains period
Dots in URL causes 404 with ASP.NET mvc and IIS

validate website string c#

I have a form that users enters there website. Problem is some users put their email address in which I do not want. I want a way to check if the url is well structured. e.g. no #, must have a root domain. www subdomains are optional. I am unable to find this anywhere.
I have tried this code
if (!Uri.TryCreate("http://" + websiteurl, UriKind.Absolute, out uri) || null == uri)
returning false on error but my problem is that it still validates without a root domain e.g. I can put in
http://websitename
and validates fine which I do not want. It does return false when I have put in
http://websitename#.
Is there a way I can overcome this problem? also I added
http:// in the passthrough value because the url never validates.
You can use:
Uri.IsWellFormedUriString(inputUrl, UriKind.RelativeOrAbsolute)
Depending on your performance needs, maybe issuing a quick HttpWebRequest for the website url they give and verifying that you get back a success response might be a good option.
You could try with a regular expression.
Uri.IsWellFormattedUriString won't solve the problem here, which includes the ability to distinguish a valid Url from an email address. Both are well formatted Uris.
Use a regular expression. Here's one from the MS forums using C#:
Url validation with Regular Expression
But you should really validate this before it gets sent to the server. If you use the Peter Blum validators, he's already done the work for you.
Peter Blum's Validators
Or if you want to put in your own JavaScript file, check out this StackOverflow thread.
Url Validation using jQuery

Why my REST API clients need JSONP request?

I created REST base webAPI in MVC 4 and hosted on server when I call this from HTML on other domain and on my local pc I need to call it as JSONP request like put callback=? in url so it can be jsonp. My question is that why this is so? if its due to cross domain then how google and facbook and other companies host their api we also call it from our own domain but we dont keep callback=? in their url.
so why my API need callback=? in url if i call it from other domain or on my local pc with simple jquery html.
Its because of the Same Origin Policy imposed by the browsers.
See
http://www.w3.org/Security/wiki/Same_Origin_Policy
http://en.wikipedia.org/wiki/Same_origin_policy
.
Also note that CORS might be a better option than JSONP in the future
http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
EDIT: ------------
If you have gone through above links you would see that JSONP allows users to work-around this Same Origin Policy security measure imposed by the browsers.
Trick is browsers allow tags to refer files in other domains than the origin.
Basically what happens is with JSONP, you send a callback function name to the server appended to the query string. Then the server will pad or prefix it's otherwise JSON request with a call to this function, hence the P in the name to denote response is padded or prefixed.
For example you can create a script tag like
then the target server, should send a response such that
mymethod({normal: 'json response'})
when this repsone is evaluated on the client side (as for any other javascript file) it will effectively call your method with the JSON response from that server.
However, this can only do GET requests.
If you want to make POST (PUT/DELETE) requests you need to use CORS in which server needs to set a specific header beforehand.
Access-Control-Allow-Origin: www.ext.site.com
Hope this helps.
Because of the same-origin policy limitations. The same-origin policy prevents a script loaded from one domain from getting or manipulating properties of a document from another domain. That is, the domain of the requested URL must be the same as the domain of the current Web page. This basically means that the browser isolates content from different origins to guard them against manipulation.

Passing network path in URL

I am creating a WCF Service with a method
[OperationContract]
[WebGet(UriTemplate = "acl/f={fullFileName}")]
string GetACL(string fullFileName);
fullFileName is a full path to a network file, or a file on the host.
The host is a Windows Service with webHttpBinding and behavior configuration.
I want to call this from a browser using something like
http://localhost/webservice/acl/f=[my network path here]
I have tried .../acl/f=file://\server\share\file.ext
.../acl/f=file://c:\file.ext
In the browser I receive "Endpoint not found".
I know this works because I can call .../acl/f=file.txt and I get back the proper response from my service indicating that the file was not found. So the method is getting called correctly when I don't use slashes of anysort in the URI.
Any thoughts on this will be greatly appreciated.
Thanks,
beezlerco at hotmail...
You need to encode the slashes, colons, and technically the periods as well.
\ should be %5C
/ should be %2F
. should be %2E
: should be %3A
for most other special characters see http://www.asciitable.com/ and use '%' plus the hex column on that table.
I believe HttpUtility.UrlEncode is what you are looking for.
(For a detailed description, see Using HttpUtility.UrlEncode to Encode your QueryStrings)

Categories