-1

I have created an webapi and send an empty string in webapi actionresult method. and have to check empty string check inside action method ,but empty string converted to below.

 [Route("{product}/{name}")]
            public IHttpActionResult GetName(string product,string Name)
            {
                var x = Name;

          if(!string.IsNullOrEmpty(x)){
          // do the logic 
          }

     else{

         }
            return Ok(true);
            }

why it is adding the empty as below format - format is "\"\""

webapi url be: http://localhost:60088/api/Name/GetName/Nokia/""

enter image description here

Mohamed Sahir
  • 2,482
  • 8
  • 40
  • 71
  • backslash is used to escape the character. Your double quote is special character and to escape it, foreach double quote character, it is replaced by \" and hence the string "" is converted to \"\" – user869375 Mar 02 '18 at 07:44
  • Possible duplicate of [Stop visual studio debug putting slash in string containing double quotes](https://stackoverflow.com/questions/41172620/stop-visual-studio-debug-putting-slash-in-string-containing-double-quotes) – CodeCaster Mar 02 '18 at 08:00

1 Answers1

0

That is because what you send is not empty but two quotation marks ("") in your url.

The double quotation mark is displayed as "\"\"" while looking it up because it is stored as a String which is introduced by an (") then followed by two escaped quotation marks (\") and then closed by another simple quotation mark.

Look here for further information on escaped Characters.

If you want to send an actual empty value you just omit that value completely.

Edit:

You might want to look up "NightOwl888's Answer on URL parameters" and/or "Web API Routing" to get yourself accustomed with Web API Routing.

Severin Jaeschke
  • 661
  • 7
  • 22
  • If you leave the value empty its not String.Empty (Equal to "") but null. So you have to check for that. – Severin Jaeschke Mar 02 '18 at 09:35
  • I have tried for empty string and null check here `!string.IsNullOrEmpty(x)` but it does not matches and falls into else condition. – Mohamed Sahir Mar 02 '18 at 09:50
  • What is the url you are calling? – Severin Jaeschke Mar 02 '18 at 09:51
  • http://localhost:60088/api/Name/GetName/"" need s to handle this scenario – Mohamed Sahir Mar 02 '18 at 09:52
  • And what is the value that is shown on inspection? Considering the example that you made in your question you should now have a String containing two quotation marks in product and null in name. – Severin Jaeschke Mar 02 '18 at 09:56
  • when i call by above link, the length of string be `"\"\"" ` 2 and it is not null . here it is not matches the above condition and goes into else statement , in my case i need to "\"\"" be consider as null or empty string which is better approach to do this. i can by pass the way when check the string length>2 means it will goes into block,but empty string i don't want to do a length or count check – Mohamed Sahir Mar 02 '18 at 10:11
  • 1
    That's what I said: You are filling in info into your parameter. Leave the quotation marks out of your url, then it is working as i described above. You can also check for a String containing double quotes but it's debatable if that's good behaviour. Also you might want to look at the two links i added to the post and read up on what you are trying to do. – Severin Jaeschke Mar 02 '18 at 10:19
  • yes , there are two options in my client requirement , one passes the value in parameter `localhost:60088/api/Name/GetName/productNames` - working fine and failed - another one is localhost:60088/api/Name/GetName/"" and the second logic gets failed because it is not considered as null, okay thanks man for the valuable comment. i got your point. – Mohamed Sahir Mar 02 '18 at 10:22