I Have a model which has DateTime type in it:
public class ToDo
{
public int id { get; set; }
public int parentId { get; set; }
public string note { get; set; }
public DateTime due { get; set; }
public ToDo(int id, int parentId, string note, DateTime due)
{
this.id = id;
this.parentId = parentId;
this.note = note;
this.due = due;
}
}
I've created a controller for this class to send my post requests through api. but I don't know how to bind DateTime type to json i've tried a request with the following body but it didn't work out:
{"parentId":1,"note":"hello world","due":{"year":2017,"month": 11,"day":25}}
How should I post the DateTime type?
Apparently one of the ways you can do it is this:
{"due": "2017-11-01T00:00:00"}
it was actually an easy question but if you want to make sure how to make a proper post request for unknown object types format it's best to send an object with empty body to see the default values.
For DateTime Type property, you need to pass the String which could be converted to DateTime Type.
For {"year":2017,"month": 11,"day":25}, it is object instead of String, it will fail to convert to DateTime.
For anything which could be converted to DateTime by Convert.ToDateTime and DateTime.Parse.
So, both {"parentId":1,"note":"hello world","due":"05/05/2005"} and {"parentId":1,"note":"hello world","due":"2018-05-10"} will work, you could make test with the DateTime string you need.
Related
I've one question on how to pass complex object to Web API GET method, where complex object has one more inner object. I've seen examples where there are no inner objects and no one is talking about the complex object which has inner object and what is the best way of achieving that. I have something which is working but I want to know if there are better ways of doing it.
Example without inner object –
public class Name
{
string FName {get; set;}
public string LName { get; set; }
}
[HttpGet]
[Route("Test")]
public void Test([FromUri]Name details)
{
Console.WriteLine(details.ToString());
}
Postman - http://localhost:XXXX/api/NameSearch/Test?FName=Soumen&LName=Banerjee
Here is the example with complext object having inner object.
Example –
public class Address
{
public string Street { get; set; }
public string Zipcode { get; set; }
}
public class Name
{
public string FName {get; set;}
public string LName { get; set; }
public Address Address { get; set; }
}
[HttpGet]
[Route("Test")]
public void Test([FromUri]Name details)
{
Console.WriteLine(details.ToString());
}
Postman - http://localhost:XXXX/api/NameSearch/Test?details.fName=Soumen&details.lName=Banerjee&details.address.street=Mains%20Height&details.address.zipcode=12345
So here you can see that, I've to pass the values using fully qualified name, property and value e.g. details.fname=Soumen or details.address.zipcode=12345. This is different than what I've done in my first example. So, I want to know are there better ways of doing this or this is the only way to pass complex object with inner object(s)? Thank you.
I am using MongoDB database with MVC4 WebAPI using the C# driver provided by MongoDB. I have a an issue with serialization. I get the following error,
"ExceptionMessage=Error getting value from '__emptyInstance' on 'MongoDB.Bson.ObjectId'"
If I change the Content-Type to xml in my HTTP request it just works fine. I would appreciate any help.
I have copied the model below.
public class Subscriber
{
public ObjectId _id;
public long SubscriberId { get; set; }
public Name Name { get; set; }
public Address Address { get; set; }
public string Phone { get; set; }
public ICollection<Subscription> Subscription { get; set; }
public Subscriber()
{
Name = new Name();
Address = new Address();
Subscription = new Collection<Subscription>();
}
}
Solution
Converting _id type string and decorating the field as below did the trick
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string _id;
reference: JSON.NET cast error when serializing Mongo ObjectId
For anyone trying the mentioned "solution" in the answer : It simply doesn't work!
Check the marked answer in this, instead.
I'm using Delta for patching an entity as outlined in "Partial Updates(PATCH request)" section outlined here
I have the following ProductDTO:
public class ProductDTO
{
public int ID { get; set; }
[Required]
public string Name { get; set; }
[UIHint("Date")]
[DataType(DataType.Date)]
public DateTime? ModifiedOn { get; set; }
public int Price { get; set; }
}
And the following action method defined:
public HttpResponseMessage Patch(int id, Delta<ProductDTO> delta)
{
return Request.CreateResponse(HttpStatusCode.NoContent);
}
If I pass in the following JSON (via Fiddler using the PATCH verb)
{"ID":1,"Name":"test","Price":"1000"}
The "delta" in the action method contains only the "Name" property and not the ID and Price properties. It appears that the delta is not containing values of types "int","decimal" and primitive types in general.
What am I missing here?
TIA
EDIT: Here is a link to a modded version of Delta that includes support for primitive JSON data types. Comments appreciated
I think I am missing a simple solution here. When I run a select (using Linq-style on IQueryable) that returns IQueryable, I may want MyClass to return differently-formatted key/values than I will use in my insert.
Consider as example the below UploadFile class and note properties id and Filetimestamp. When I SET Filetimestamp I want it stored as DateTime, but when I RETRIEVE IQueryable I would like to:
See Filetimestamp returned as String
OR
See a separate property returned as String reflecting Filetimestamp.toString(). This property doesn't have a corresponding key in the database; it's just a decoration of a real key/value from BSON.
Thinking #1 was unlikely, I pursued #2 by adding a get-only field Filetimestamp_str that returns Filetimestamp.toString(). When I select, behavior is as expected. When I insert, I see failures.
I could create a decorator class that I use for gets and all would work - but am I missing something more fundamental and simple here?
public class UploadFile : IUploadFile
{
public Object _id { get; set; }
public String Filepath { get; set; }
public String Filename { get; set; }
public String Filetype { get; set; }
public String Fileauthor { get; set; }
public DateTime Filetimestamp { get; set; }
public Object FileID { get; set; }
// these keys are not in DB
public String Filetimestamp_str { get { return Filetimestamp.ToString(); } }
public String _id_str { get { return _id.ToString(); } }
}
You could create a BsonClassMap, specifying which properties should (not) be serialized. See the Serialization Tutorial. However, according to that tutorial, a read-only property should not be automapped, so I should not expect errors on the insert.
This is a follow up to this:
What does MVC3 do with C# Optional Parameters?
I have an action with the following signature:
public ViewResult Show(int Id, PublishingErrorSummary pubErrors=null, String title=null)
On requesting server/show/1 pubErrors is not null, but title is null. How is it possible? These are just two objects but string somehow manages to become null. Where can I fix this?
Edit: class definition added
public class PublishingErrorSummary
{
public List<string> StepOneErrors { get; set; }
public List<string> StepTwoErrors { get; set; }
public List<string> StepThreeErrors { get; set; }
public List<string> StepFourErrors { get; set; }
}
PublishingErrorSummary is a complex object. The default model binder always initializes complex objects. It doesn't really make sense to set its default value to null. Same stands for the title parameter. Strings are reference types and their default value will be null anyway if no request parameter title is sent.