Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I've two get methods in my WebAPI, which are:

C#
CheckPassword(string Password, string regExp) //to validate password pattern, takes password and the regular expression.


C#
CheckEmailValid(string Email, string regExp) //to validate email pattern, takes email and its regular expression.


When I try to call a Get method in WebAPI by passing two strings(password, regExp) OR (email, regExp) using a console app, some of the character miss out from the string. But at the same time if I have the regular expression in my WebAPI and test, it works fine. So I miss few character somewhere between the application and the webAPI.

Original Regex string for Password

C#
public const string passwordPattern = @"^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]{7,}$"


The string which I receive at WebAPI Method while debugging

C#
{^(?=.*[0-9] .*)(?=.*[a-zA-Z] .*)[0-9a-zA-Z]{7,}$}

Here I'm missing out + symbol, thats why password validation fails all the time.

Original Regex string for Email

C#
public const string emailPattern = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";

The string which I receive at WebAPI Method while debugging

C#
\\A(?:[a-z0-9!

Any help will be greatly appreciated.

What I have tried:

I tried storing the regular expression text in a constant variable and pass, but still same characters where missing.
Posted
Updated 30-May-16 0:37am
Comments
Peter_in_2780 30-May-16 3:09am    
You are the victim of http's quirks. Look up "URL encoding". Basically you'll need to hide things like your + symbol. A quick and easy way would be to base-64 encode your strings and base-64 decode on receipt.
Prateek Dalbehera 30-May-16 3:47am    
Agree with Peter, So Juli, you try the following code to avoid any issues,

HttpUtility.UrlEncode(userParam.Email)

While sending from controller to API, send like above, In your case, it will be like,

HttpUtility.UrlEncode(passwordPattern)
HttpUtility.UrlEncode(emailPattern)
Jilu K Thomas 30-May-16 6:05am    
Thank you Peter & Prateek. That really helped.
Prateek Dalbehera 30-May-16 6:37am    
u r mst welcm...

1 solution

Hi Jilu,
If you want to avoid any html encoding issues while sending data, you can always use below encoding method of C#.
Also, it will come very handy in case of query strings as well.

You can apply this code while sending from browser in forms of Query String also.

C#
HttpUtility.UrlEncode(passwordPattern)
HttpUtility.UrlEncode(emailPattern)


Please mark as answer if it works.

Regards,
Prateek
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900