ASP.NET WebApi form-urlencoded string deserialize - asp.net-mvc-4

I develop service with WebApi and Client which send to this service gzipped data by POST method (to save bandwidth). Both of them over my control. On server I receive compressed data, decompress It and have string such as:
section[0][caption]=Foo&
section[0][address]=175896&
section[0][counters][]=2&
section[0][counters][]=2&
section[0][errors][]=ERR_NOT_AVAILABLE&
errors=true&
trmtimestamp=1346931864358
ie simple www-form-urlencoded string.
Does ASP.NET MVC4 WebApi have some method to bind or deserialize this string to my Model?
public class Snapshot
{
public List<SectionState> sections { get; set; }
public bool errors { get; set; }
public double date { get; set; }
public double trmtimestamp { get; set; }
}

You may use the following code to parse the string:
new System.Net.Http.Formatting.FormDataCollection(query).ReadAs<Snapshot>();
The ReadAs is extension method defined under System.Web.Http.ModelBinding.FormDataCollectionExtensions.

Related

Postman. Issue with multipart/form-data (415 Unsupported Media Type)

I got some issue during test my endpoint (with multipart/form-data) on Postman.
I have following method (some fields + photo). This method works with Swagger ok.
[HttpPost]
public async Task<bool> MailPhoto([FromForm] MailwithPhoto mailWithPhoto)
{...}
public class MailwithPhoto
{
public string mail_message { get; set; }
public IFormFile photo_file { get; set; }
public string userContact { get; set; }
public string category { get; set; }
public string userName { get; set; }
public string method { get; set; }
}
enter image description here
First need to change Key message to mail_message,or mail_message will not be binded.
415 is often due to Content-Type or Content-Encoding, or as a result of inspecting the data directly.
Here is a working demo(I use a .net core mvc project):
result:

Sending byte array to API

I am converting web application to mobile app. We were using AjaxAsyncFileUpload in web application to save a document to the server, where AjaxAsyncFileUpload use to do the work for me. This was the code
Dim fileData As Byte() =new Byte(AjaxAsyncFileUpload.FileContent.Length-1){}
AjaxAsyncFileUpload.FileContent.Read(fileData, 0, fileData.Length)
InvestmentDeclare.DocSize = AjaxAsyncFileUpload.FileContent.Length
InvestmentDeclare.DocFileName = AjaxAsyncFileUpload.FileName
InvestmentDeclare.DocFileType = AjaxAsyncFileUpload.PostedFile.ContentType
InvestmentDeclare.Document = fileData
And then simply save this to my database.
Now while Converting this to mobile app (I am also using c# for mobile app), I am not able to pass the byte array. I am using fiddler for testing.
I have attached an image of how I'm passing it through fiddler. In my API POST method I'm getting a null value to my document variable while I'm able to get rest of my values properly.
What could be the issue? Am I not passing the byte in proper Json format?
In API:
public class AddInvestmentDeclare
{
public int EmployeeId { get; set; }
public int YearId { get; set; }
public int InvestmentId { get; set; }
public List<EmpDocument> EmpDocuments { get; set; }
}
public class EmpDocument
{
public byte[] Document { get; set; }
public string DocumentFileName { get; set; }
public long DocumentSize { get; set; }
public string DocumentType { get; set; }
}
public HttpResponseMessage Post(int YearId, [FromBody]List<AddInvestmentDeclare> InvestmentDeclared)
{
When I check my InvestDeclared list on run time I see that document variable is not filled and it is showing null. I have attached an image of that as well.
First, setup your API method in a way that it accepts just one object containing all data instead of each parameter individually.
Instead of:
public HttpResponseMessage Post([FromUri]string AccordDbName, [FromUri]String PayCareDbName, [FromUri]...)
Set it up like:
public HttpResponseMessage Post(Data dataObject)
Secondly, try sending file as a base64string.
Here is example how to do it.
Your file in JSON request should look something like this:
{ "ID":46,
"Content":"JVBERi0xLjQNCjEgMCBvYm......"
}

Unable to send image file as part Datacontract

I am developing a Wcf Restful Service which contains data contract "User" shown below
[DataContract]
public class User
{
public User()
{
}
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Mobile")]
public string Mobile { get; set; }
[DataMember(Name = "Email")]
public string Email { get; set; }
[DataMember(Name = "IsImageUpdated")]
public bool IsImageUpdated { get; set; }
}
Now i would like to add one mode data member of type Image,When i try to add Image with type Stream it showing exception
[DataMember(Name = "Iamge")]
public Stream Image { get; set; }
"The InnerException message was 'Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details."
The service i am developing having many Data contract's,I read some posts which saying the issue can be resolved by changing the Datacontract to message contract,Does a service supports different contract types(like Data,Message).
i need a solution.
This is not possible when using a WebHttpBinding.
Combining streamed and buffered Content is only possible when the binding has a SOAP message Format and you use MessageContract instead of DataContract.
Using a byte[] or returning the stream directly is supported.
[DataMember(Name = "Iamge")]
public byte[] Image { get; set; }
or
[OperationContract]
[WebGet(UriTemplate = "/Image")]
Stream GetImage();
or when using NetTcpBinding, WsHttpBinding, BasicHttpBinding, ...
[MessageContract]
public class ImageData
{
[MessageBodyMember]
public Stream Image { get; set; }
[MessageHeader]
public string Name { get; set; }
}

BreezeJS Get Metadata from the server without using Entity Framework for complex objects (nested objects)

This is my model on the server side. I don't want to use Entity Framework, how would I generate BreezeJS metadata from the server. Breeze Metadata Format found here http://www.breezejs.com/documentation/breeze-metadata-format doesn't work.
public class User
{
public string Id { get; set; }
public string Name { get; set; }
public List<App> Apps { get; set; }
}
public class App
{
public string Id { get; set; }
public string Name { get; set; }
public Dictionary<string, string> Info { get; set; }
}
Did anyone try complex objects (nested object) similar to above one without using EF.
Using Breeze Metdata API or OData ?
Look at this example http://www.breezejs.com/samples/nodb it should give you a clue.

Rolling my own CMS with the WCF, what should my contract be?

I'm looking to create a solution for a asp.net mvc app that displays unstructured course descriptions that have been scraped from around the web. The web server will be on a web hosting company while the description db will be on a private network, so I'd like to pass the description to the mvc app through a WCF service. For the body of the description do I just want to use a string property...
public class CourseDescription {
// data fields
public string CourseTitle { get; set; }
public int Days { get; set; }
public List<DateTime> UpcomingDates { get; set; }
// html properties
public string Keywords { get; set;}
public string HtmlDescription { get; set; }
}
... or is there a better way?
If it is unstructured text, a string is fine.