2

I'm passing values from a C# application to an ASP.NET MVC Web API that's been created. I've learned that when my value includes "\" like the the following:

{ id:"1", path:"D:\Backup\DataSource\MyFile.txt", name:"test" }

that the parameter in MyAction is null.

[HttpPost]
public CustomResponse MyAction(CustomEntity entity)
{
  ... // do stuff
}

public class CustomEntity
{
  public string id { get; set; }
  public string path { get; set; }
  public string name { get; set; }
}

If the path does NOT include "\" everything works as intended. I'm guessing this is some type of encoding issue. However, I'm not sure what kind of encoding I need to use before I send the values from my C# app to the Web API.

What kind of encoding do I need to use?

Bill Jones
  • 701
  • 4
  • 16
  • 24
  • 1
    Try using double slashes. \\ instead of \. I think you need to escape the escape character. – Steven V Mar 12 '13 at 20:32
  • I did that. But that's not my concern. I'm more concerned there are other characters that I need to consider that I'm not. That's why I'm asking about encoding. – Bill Jones Mar 12 '13 at 20:59
  • Take a look at http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx#file_and_directory_names for the actual list of rules about paths. In C# you can get an array of characters you would to escape file names at http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidfilenamechars.aspx and paths at http://msdn.microsoft.com/en-us/library/system.io.path.getinvalidpathchars.aspx. – Steven V Mar 12 '13 at 21:22
  • Check out [this link](http://stackoverflow.com/questions/13771032/post-string-to-asp-net-web-api-application-returns-null) - I think it might point you in the right direction. – Troy Alford Apr 24 '13 at 20:07

0 Answers0