My project is brand new Asp.net 2015 MVC6 beta 8 web application.
I get value as null when I call Web api with post type from C# code.
My server side code:
// POST api/values
[HttpPost]
public void Post([FromBody]string value)
{
if( null != value )
do something;
}
My clientside is:
StringContent cstrJson = new StringContent("{ mesage: hello}"
, System.Text.Encoding.Unicode, "application/x-www-form-urlencoded");
var result = await client1.PostAsync("http://localhost:68888/api/myApi/", cstrJson);
I tried all different combinations of encoding and media, but no improvements.
It's null because the body couldn't be parsed as a string. The content type is application/x-www-form-urlencoded instead of text/plain.
You may want to rethink using a string anyway if your client is sending json, you should accept application/json on the server and let the framework parse it for you.
[HttpPost]
public void Post(MyObject value)
{
var msg = value.Message;
}
public class MyObject
{
public string Message { get; set; }
}
Client Side:
var cstrJson = new StringContent("{'Message' : 'hello'}", System.Text.Encoding.Unicode, "application/json");
var result = await client1.PostAsync("http://localhost:68888/api/myApi/", cstrJson);
Related
I am trying to POST an object from a WebJob to an MVC 4 controller. I am using Entity Framework. In the controller, I cannot get the object to bind properly (the argument is null). I have looked at many tutorials and it seems like my code should work.
Model (does this need to be in a specific namespace for EF to find it?):
public class CreateListingObject
{
public Listing listing;
public List<GalleryImage> images;
public CreateListingObject()
{
listing = new Listing();
images = new List<GalleryImage>();
}
}
public struct GalleryImage
{
public string picURL;
public string caption;
}
POST:
public void PostListing(CreateListingObject o)
{
Console.WriteLine("Posting listing: {0}", o.listing.Title);
HttpClient _httpClient = new HttpClient();
Uri uri = new Uri(_serviceUri, "/Automaton/CreateTestListing");
string json = BizbotHelper.SerializeJson(o);
HttpResponseMessage response = BizbotHelper.SendRequest(_httpClient, HttpMethod.Post, uri, json);
string r = response.Content.ReadAsStringAsync().Result;
response.EnsureSuccessStatusCode();
}
SendRequest (thank you Azure search samples):
public static HttpResponseMessage SendRequest(HttpClient client, HttpMethod method, Uri uri, string json = null)
{
UriBuilder builder = new UriBuilder(uri);
//string separator = string.IsNullOrWhiteSpace(builder.Query) ? string.Empty : "&";
//builder.Query = builder.Query.TrimStart('?') + separator + ApiVersionString;
var request = new HttpRequestMessage(method, builder.Uri);
if (json != null)
{
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
return client.SendAsync(request).Result;
}
Controller Action fragment (o is an empty object here):
[HttpPost]
public ActionResult CreateTestListing(CreateListingObject o)
{
Listing li = o.listing;
I have confirmed that if I post a simple object using the same code, everything works as expected.
Instead of sending a CreateListingObject in PostListing, I send this instead:
var test = new
{
data = "hi mom"
};
And change my action to, then the argument gets bound and I get valid data:
[HttpPost]
public ActionResult CreateTestListing(string data)
{
I have also checked the serialization of my CreateListingObject in the WebJob, and it is fully populated as I expect. This leads me to suspect that I am falling afoul of the default ModelBinder.
I'm trying to create a LiveConnectClient with only a refresh token that was provided to me via asp.net identity (using OWIN) and the ProviderKey. It looks like the only way to do this without needing HttpContextBase is via InitializeSessionAsync.
When I try and create the client I'm getting:
Microsoft.Live.LiveAuthException: The user ID from the given RefreshTokenInfo instance does not match the refresh token.
Not really sure what user ID it is expecting as I'm giving it the provider key that was passed via ASP.NET Identity (17 chars in my case). Below is my code.
public class Class1
{
protected async Task<LiveConnectClient> GetLiveConnectClient()
{
var authClient = new LiveAuthClient(_clientId, _clientSecret, null, new RefreshTokenHandler(_refreshToken, _providerKey));
var session = await authClient.InitializeSessionAsync("http://.../signin-microsoft");
return new LiveConnectClient(session.Session);
}
}
public class RefreshTokenHandler : IRefreshTokenHandler
{
private readonly string _refreshToken;
private readonly string _userId;
public RefreshTokenHandler(string refreshToken, string userId)
{
_refreshToken = refreshToken;
_userId = userId;
}
public Task<RefreshTokenInfo> RetrieveRefreshTokenAsync()
{
return Task.FromResult(new RefreshTokenInfo(_refreshToken, _userId));
}
public async Task SaveRefreshTokenAsync(RefreshTokenInfo tokenInfo)
{
await Task.Delay(0);
}
}
I have a knockout/mvc3 application. I am passing the date back to a controller.
controller
public ActionResult PackageUpdate(Package updatePackage){
\\do some stuff but dates are set to zero?
}
view model and save method
var Package = function (data) {
self = this;
self = ko.mapping.fromJS(data);
self.save = function(){
$.ajax({
url: '/MediaSchedule/PackageUpdate',
data:ko.toJSON({ updatePackage: self })
}).success(function (results) {
console.log(results);
}).error(function (er) {
console.error('Ah damn you broke it.')
console.log(er);
});
}
return self;
}
Json Being passed.
{"updatePackage":{"Id":"82e3bc7e-27b8-49c2-b1fa-1ee2ebffbe66","Name":"28a38","SecondaryName":"è€å我è¦é’±","IsLocked":true},"DateCreated":"/Date(1357650000000+1100)/","DateStart":"/Date(1365080400000+1100)/","DateEnd":"/Date(1365516000000+1000)/"}
ID, name and other properties are coming through but the date is being reset to {1/1/0001 12:00:00 AM}. My assumption is because it is not being deserialised it is setting a min date. Question: How do I correctly deserialise my date.
I think the problem lies in how you obtaining those dates to begin with. You show an example using MS date format such as /Date(1357650000000+1100)/ which is not standardized and is slowly being deprecated in favor of ISO8601, which looks like 2013-01-08T13:00:00.000+11:00.
Indeed, when you JSON.stringify a javascript Date object, it uses the ISO8601 format. This also happens with ko.mapping.toJSON.
There are several solutions to this problem, both client side and server side. This post describes the problem in detail and has some great answers that may help you.
IMHO, the best solution is have your MVC controller emit and consume ISO8601 rather than the old Microsoft date format. The easiest way to do that is by using the Json.Net library which now has ISO8601 as the default so you don't even need to customize it. On the client side, you might also want to look at Moment.js - which makes it easy to parse and format ISO dates.
I think it just the data type you push to you updatePackage object.
Following is my code and run well, I use read the Date from jQuery Datepicker and use the format as 'dd MM, yy' (01 January 2013)
var iEndInsuredDate = $('#dpkEndInsuredDate').val();
var iEndPolicyDate = $('#dpkEndPolicyDate').val();
$.ajax({
url: '#Url.Action("DeleteClientMember", "ClientMember")',
type: "POST",
dataType: "json",
data: { clientMemberID: id, endInsuredDate: iEndInsuredDate, endPolicyDate: iEndPolicyDate },
success: function (result) {
ShowWaiting("Reloading...");
Search(1);
}
});
and my ActionResult
public ActionResult DeleteClientMember(int clientMemberID, DateTime? endInsuredDate, DateTime? endPolicyDate)
{
ClientMember model = clientMemberService.GetById(clientMemberID);
//model.EndPolicyDate = endPolicyDate;
model.EndInsuredDate = endInsuredDate;
foreach (ClientMemberProduct item in model.ProductList)
{
item.EndDate = endInsuredDate;
}
model.IsActive = false;
model.ActionStatus = ClientMemberActionStatus.PendingDelete.ToString();
clientMemberService.CalculateInsFee(model);
clientMemberService.Update(model);
return null;
}
Hope this help
Regard
Thanks to Matt Johnson I was able to change the way the dates were being sent to the browser. It was a relativly easy fix taken from Perishable Dave answer to a similar problem
with ASP.NET MVC JsonResult Date Format
My JsonNetResult class now look like
public class JsonNetResult : ActionResult
{
private const string _dateFormat = "yyyy-MM-dd hh:mm:ss";
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public JsonNetResult()
{
SerializerSettings = new JsonSerializerSettings();
Formatting = Formatting.Indented;
SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType)
? ContentType
: "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data != null)
{
var isoConvert = new IsoDateTimeConverter();
isoConvert.DateTimeFormat = _dateFormat;
JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting };
JsonSerializer serializer = JsonSerializer.Create(SerializerSettings);
serializer.Converters.Add(isoConvert);
serializer.Serialize(writer, Data);
writer.Flush();
}
}
}
I have added the iso date converter to the serizer
in the controller you invoke it by:
public JsonNetResult YourAction(){
//your logic here
return JsonNetResult(/*your object here*/);
}
When I wrote this I did not know about Web API. It is worth a look as it will do a lot of the heavy lifting when it comes to serialization of your objects. Checkout Getting Started with ASP.NET Web API 2
In an effort to make a progress reporting process a little more reliable and decouple it from the request/response, I am performing the processing in a Windows Service and persisting the intended response to a file. When the client starts polling for updates, the intention is that the controller returns the contents of the file, whatever they are, as a JSON string.
The contents of the file are pre-serialized to JSON. This is to ensure that there is nothing standing in the way of the response. No processing needs to happen (short of reading the file contents into a string and returning it) to get the response.
I initially though this would be fairly simple, but it is not turning out to be the case.
Currently my controller method looks thusly:
Controller
Updated
[HttpPost]
public JsonResult UpdateBatchSearchMembers()
{
string path = Properties.Settings.Default.ResponsePath;
string returntext;
if (!System.IO.File.Exists(path))
returntext = Properties.Settings.Default.EmptyBatchSearchUpdate;
else
returntext = System.IO.File.ReadAllText(path);
return this.Json(returntext);
}
And Fiddler is returning this as the raw response
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 19 Mar 2012 20:30:05 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 81
Connection: Close
"{\"StopPolling\":false,\"BatchSearchProgressReports\":[],\"MemberStatuses\":[]}"
AJAX
Updated
The following will likely be changed later, but for now this was working when I was generating the response class and returning it as JSON like a normal person.
this.CheckForUpdate = function () {
var parent = this;
if (this.BatchSearchId != null && WorkflowState.SelectedSearchList != "") {
showAjaxLoader = false;
if (progressPending != true) {
progressPending = true;
$.ajax({
url: WorkflowState.UpdateBatchLink + "?SearchListID=" + WorkflowState.SelectedSearchList,
type: 'POST',
contentType: 'application/json; charset=utf-8',
cache: false,
success: function (data) {
for (var i = 0; i < data.MemberStatuses.length; i++) {
var response = data.MemberStatuses[i];
parent.UpdateCellStatus(response);
}
if (data.StopPolling = true) {
parent.StopPullingForUpdates();
}
showAjaxLoader = true;
}
});
progressPending = false;
}
}
The issue, I believe, is that the Json action result is intended to take an object (your model) and create an HTTP response with content as the JSON-formatted data from your model object.
What you are passing to the controller's Json method, though, is a JSON-formatted string object, so it is "serializing" the string object to JSON, which is why the content of the HTTP response is surrounded by double-quotes (I'm assuming that is the problem).
I think you can look into using the Content action result as an alternative to the Json action result, since you essentially already have the raw content for the HTTP response available.
return this.Content(returntext, "application/json");
// not sure off-hand if you should also specify "charset=utf-8" here,
// or if that is done automatically
Another alternative would be to deserialize the JSON result from the service into an object and then pass that object to the controller's Json method, but the disadvantage there is that you would be de-serializing and then re-serializing the data, which may be unnecessary for your purposes.
You just need to return standard ContentResult and set ContentType to "application/json".
You can create custom ActionResult for it:
public class JsonStringResult : ContentResult
{
public JsonStringResult(string json)
{
Content = json;
ContentType = "application/json";
}
}
And then return it's instance:
[HttpPost]
public ActionResult UpdateBatchSearchMembers()
{
string returntext;
if (!System.IO.File.Exists(path))
returntext = Properties.Settings.Default.EmptyBatchSearchUpdate;
else
returntext = Properties.Settings.Default.ResponsePath;
return new JsonStringResult(returntext);
}
Yeah that's it without no further issues, to avoid raw string json this is it.
public ActionResult GetJson()
{
var json = System.IO.File.ReadAllText(
Server.MapPath(#"~/App_Data/content.json"));
return new ContentResult
{
Content = json,
ContentType = "application/json",
ContentEncoding = Encoding.UTF8
};
}
NOTE: please note that method return type of JsonResult is not working for me, since JsonResult and ContentResult both inherit ActionResult but there is no relationship between them.
Use the following code in your controller:
return Json(new { success = string }, JsonRequestBehavior.AllowGet);
and in JavaScript:
success: function (data) {
var response = data.success;
....
}
All answers here provide good and working code. But someone would be dissatisfied that they all use ContentType as return type and not JsonResult.
Unfortunately JsonResult is using JavaScriptSerializer without option to disable it. The best way to get around this is to inherit JsonResult.
I copied most of the code from original JsonResult and created JsonStringResult class that returns passed string as application/json. Code for this class is below
public class JsonStringResult : JsonResult
{
public JsonStringResult(string data)
{
JsonRequestBehavior = JsonRequestBehavior.DenyGet;
Data = data;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Get request is not allowed!");
}
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
response.Write(Data);
}
}
}
Example usage:
var json = JsonConvert.SerializeObject(data);
return new JsonStringResult(json);
I am working on an MVC3 application. My client side ViewModel contains a SQL Server RowVersion property, which is a byte[]. It is rendered as an Object array on the client side. When I attempt to post my view model to a controller, the RowVersion property is always null.
I am assuming that the Controller serializer (JsonValueProviderFactory) is ignoring the Object array property.
I have seen this blog, however this does not apply, as I am posting JSON and not the form markup:
http://thedatafarm.com/blog/data-access/round-tripping-a-timestamp-field-with-ef4-1-code-first-and-mvc-3/
My view renders my viewmodel like so:
<script type="text/javascript">
var viewModel = #Html.Raw( Json.Encode( this.Model ) );
</script>
I then post the viewModel to the controller like so:
var data = {
'contact': viewModel
};
$.ajax({
type: 'POST',
url: '/Contact/Save',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data),
dataType: 'json',
success: function (data) {
// Success
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
});
Here is my action in the controller:
[HttpPost]
public JsonResult Save(Contact contact) {
return this.Json( this._contactService.Save( contact ) );
}
UPDATE: based on Darin's answer.
I was hoping for a cleaner solution, but since Darin provided the only answer, I will have to add a custom property that will serialize my byte[] "row_version" property to a Base64 string. And when the Base64 string is set to the new custom property, it converts the string back to a byte[]. Below is the custom "RowVersion" property that I added to my model:
public byte[] row_version {
get;
set;
}
public string RowVersion {
get {
if( this.row_version != null )
return Convert.ToBase64String( this.row_version );
return string.Empty;
}
set {
if( string.IsNullOrEmpty( value ) )
this.row_version = null;
else
this.row_version = Convert.FromBase64String( value );
}
}
My client side ViewModel contains a SQL Server RowVersion property, which is a byte[]
Make it so that instead of a byte[] your view model contains a string property which is the base64 representation of this byte[]. Then you won't have any problems roundtripping it to the client and back to the server where you will be able to get the original byte[] from the Base64 string.
Json.NET automatically encodes byte arrays as Base64.
You can use JsonNetResult instead of JsonResult:
from https://gist.github.com/DavidDeSloovere/5689824:
using System;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class JsonNetResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(this.ContentType) ? this.ContentType : "application/json";
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data == null)
{
return;
}
var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var formatting = HttpContext.Current != null && HttpContext.Current.IsDebuggingEnabled ? Formatting.Indented : Formatting.None;
var serializedObject = JsonConvert.SerializeObject(Data, formatting, jsonSerializerSettings);
response.Write(serializedObject);
}
}
Usage:
[HttpPost]
public JsonResult Save(Contact contact) {
return new JsonNetResult { Data = _contactService.Save(contact) };
}