title: URL Encoding in c# and Asp.net web api
body: <p>I have an <code>ASP.NET web api</code> that receives web requests and returns <code>Json</code> data.<br>
  browsing to this URL:<br>
  <code>http://1.2.3.4/api1/api/values/mypartname</code> will return the following json string: </p>
  
  <pre><code>{
      \"PartName\": \"mypartname\",
      \"PartDes\": \"53.6X53.6APA/ALIM1NOTPAK\",
      \"PartLocation\": \"A36\"
  }
  </code></pre>
  
  <p>but when sending a part name that contains spaces or quotes like this: <code>http://1.2.3.4/api1/api/values/my part na"me</code> i get a <code>404 - File or directory not found.</code> error.<br>
  I'm consuming the <code>json</code> with a <code>.NET 4 Console application</code> like so:  </p>
  
  <pre><code>static void Main(string[] args)
      {
          try
          {
              string partName = "TAPE 56 3M 3/4\"";
              WebRequest wr = WebRequest.Create("http://1.2.3.4/api1/api/values/" +
                  HttpUtility.UrlEncode(partName));
              wr.Credentials = CredentialCache.DefaultCredentials;
              HttpWebResponse hwr = (HttpWebResponse)wr.GetResponse();
              Stream dataStream = hwr.GetResponseStream();
              StreamReader reader = new StreamReader(dataStream);
              string json = reader.ReadToEnd();
              //some json parsing function
              des(json);
              reader.Close();
              dataStream.Close();
              hwr.Close();
  
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
          finally
          {
              Console.ReadKey();
          }
      }
  </code></pre>
  
  <p>the exception is thrown at this line:<code>HttpWebResponse hwr = (HttpWebResponse)wr.GetResponse();</code>
  and the exception message is: <code>The remote server returned an error: (404) Not Found.</code></p>
  
  <p>Am i doing something wrong with the <code>mypartname</code>? I also tried to manually replace the problematic characters according to this: <a href="http://www.w3schools.com/tags/ref_urlencode.asp" rel="nofollow">HTML URL Encoding Reference</a> and using this function:<code>Uri.EscapeDataString(partName)</code> but with no luck.</p>
  
  <p><strong>EDIT</strong><br>
  this is the routeConfig definition:  </p>
  
  <pre><code>        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );  
  </code></pre>
  
  <p>and the api GET method:  </p>
  
  <pre><code>// GET api/values/5
      public string Get(string id)
      {
  
          List&lt;dummy&gt; dummies = new List&lt;dummy&gt;();
          string con = "user id=sa;" +
                       "password=1234" +
                       "server=someServer\\someInstance;" +
                       "database=game; " +
                       "connection timeout=30";
  
          //SqlConnection sqlConn = new SqlConnection(con);
          using (SqlConnection sqlconn = new SqlConnection(con))
          {
              sqlconn.Open();
              StringBuilder sb = new StringBuilder();
              sb.Append("SELECT PART.PARTNAME,PART.PARTDES, PARTPARAMA.LOCATION ");
              sb.Append("FROM PART LEFT JOIN PARTPARAMA ");
              sb.Append("ON PART.PART = PARTPARAMA.PARTPARAM ");
              sb.Append("WHERE PART.PARTNAME = @part");
  
  
              using (SqlCommand cmd = new SqlCommand(sb.ToString(), sqlconn))
              {
                  cmd.Parameters.AddWithValue("part", id);
                  SqlDataReader sdr = cmd.ExecuteReader();
                  while (sdr.Read())
                  {
  
                      dummies.Add(new dummy
                      {
                          PartName = sdr.IsDBNull(0) ? "Unknown" : sdr.GetString(0),
                          PartDes = sdr.IsDBNull(1) ? "Unknown" : sdr.GetString(1),
                          PartLocation = sdr.IsDBNull(2) ? "Unknown" : sdr.GetString(2)
                      });
                  }
              }
          }
  
          if (dummies.Count() &gt; 0)
          {
              string json = JsonConvert.SerializeObject(dummies[0]);
              return json;
          }
          else
          {
              string json = JsonConvert.SerializeObject(null);
              return json;
          }
  </code></pre>