0

I have a problem to post a content with & like "M&A is the trend....",the message only stop at M

MVC Web API

  [HttpPost]
        public IHttpActionResult Post([FromUri] CommentModel param)
        {}

    public class CommentModel
    {       
        public int Id { get; set; }
        public string Message { get; set; }
        ...
    }

Web Api config:

config.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { id = RouteParameter.Optional }

           );

Call it from angularJS:

 var message = encodeURI($scope.message);   
 var request = $http.post('api/comment/?message=' + message);

....
nam vo
  • 3,271
  • 12
  • 49
  • 76

2 Answers2

2

Encode content before sending.

M&A is the trend....

After encode look like this:

M&A is the trend....
Nguyen Kien
  • 1,897
  • 2
  • 16
  • 22
2

Ideally, when you perform an HTTP/POST you send the data as an entity as part of request body (not in the URI).

That is, your API controller's method should look as follows:

public IHttpActionResult Post(CommentModel param)

And your HTTP/POST request should both contain a Content-type: application/json header and a JSON entity as part of request body (again, not in the URI!!!!).

Finally, if you need to send ampersands as data in your resource URIs or url-form-encoded data, you'll need to encode them since ampersands have specific meaning on this context (they are the key-value pair separator). If you're performing requests from an HTML5 App, you can encode your parameters usingencodeURIComponent(...) function, and in .NET you can use Uri.EscapeDataString.

POST verb is meant to create a resource and you send the resource data as an entity (it could be JSON, XML, url-form-encoded, or whatever, but ideally not in the URI as a query string).

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206