We have asp.net core 6 get rest apis where we are returning the list wrap with content result class. I know we can stream data in asp.net core web apis with IAsyncEnuerable but I wanted to know how we can do that with the following class returned.
public class ContentResultList<T>
{
public bool IsSuccess { get; set; }
public string Message { get; set; }
public List<T> Data { get; set; }
public int TotalPages { get; set; } = 0;
public int CurrrentPage { get; set; } = 1;
public int PageSize { get; set; } = 10;
public int TotalNumberOfRecords { get; set; } = 0;
public int RecordsTotal { get; set; }
public int RecordsFiltered { get; set; }
public T Result { get; set; }
}
Here you see that we have List Data but here I want to have IAsyncEnumerable and stream that data in the API. Is this possible
Thanks in advance!
Related
I have a Xamarin project which consumes an Api. I have an entity CrProduct and on my database I store 1000 products but the limit I set on my Api per page is 50.
My Api Response returns 2 entities, Data and Meta.
Here's my Meta entity
public class Meta
{
public int TotalCount { get; set; }
public int PageSize { get; set; }
public int CurrentPage { get; set; }
public int TotalPages { get; set; }
public bool HasNextPage { get; set; }
public bool HasPreviousPage { get; set; }
public string NextPageUrl { get; set; }
public string PreviousPageUrl { get; set; }
}
And this is what I do on my Get method. I just send the pageSize but I want to do this properly, without guessing how many products there will be.
public async Task<BaseMetaEntity<CrProduct>> GetProductsByCompany(int company, int pageSize, string token)
{
string urlPagination = string.Concat(Constants.UrlProduct, "?IdCompany=", company, "&PageSize=", pageSize);
return await _apiService.HttpGetAsync<BaseMetaEntity<CrProduct>>(urlPagination, token);
}
Here's my BaseMetaEntity
public class BaseMetaEntity<T>
{
public List<T> Data { get; set; }
public Meta Metadata { get; set; }
}
When I consume my Api is there any way to get the total count and use it on the request?
Please help and thanks.
I've been searching around for a long while for this, I haven't found any solutions to my issue which is:
I've been trying get a json data individually from a whole source seen here:
{"TargetId":0,"ProductType":null,"AssetId":1239281845,"ProductId":0,"Name":"❤️🍀𝐒𝐀𝐋𝐄❗️🍀❤️ Red&Black Flannel + Backpack","Description":"Shirt Image","AssetTypeId":1,"Creator":{"Id":124026176,"Name":"TheDestroyerPeter","CreatorType":"User","CreatorTargetId":124026176},"IconImageAssetId":0,"Created":"2017-12-12T19:48:24.693Z","Updated":"2017-12-12T19:48:24.693Z","PriceInRobux":null,"PriceInTickets":null,"Sales":0,"IsNew":false,"IsForSale":false,"IsPublicDomain":false,"IsLimited":false,"IsLimitedUnique":false,"Remaining":null,"MinimumMembershipLevel":0,"ContentRatingTypeId":0}
now what I've been trying to do with it is get the Product Name using C# and the product name is "❤️🍀𝐒𝐀𝐋𝐄❗️🍀❤️ Red&Black Flannel + Backpack", my issue is that I haven't found a way to extract the data, and when I have I haven't been able to get the right data, because instead if gives me "TheDestroyerPeter"
I've written up code, and deleted it, it was really sloppy and it would take awhile to rewrite, I appreciate any solutions
-whoever I am
You can use JavaScriptSerializer class, which is part of the System.Web.Script namespace.
For example :
var jsonString = #"{""name"":""John Doe"",""age"":20}";
var JSONObj = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(jsonString );
and then JSONObj["name"]; gives you "John Doe"
in this case you can use it :
public class Creator
{
public int Id { get; set; }
public string Name { get; set; }
public string CreatorType { get; set; }
public int CreatorTargetId { get; set; }
}
public class RootObject
{
public int TargetId { get; set; }
public object ProductType { get; set; }
public int AssetId { get; set; }
public int ProductId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int AssetTypeId { get; set; }
public Creator Creator { get; set; }
public int IconImageAssetId { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public object PriceInRobux { get; set; }
public object PriceInTickets { get; set; }
public int Sales { get; set; }
public bool IsNew { get; set; }
public bool IsForSale { get; set; }
public bool IsPublicDomain { get; set; }
public bool IsLimited { get; set; }
public bool IsLimitedUnique { get; set; }
public object Remaining { get; set; }
public int MinimumMembershipLevel { get; set; }
public int ContentRatingTypeId { get; set; }
}
use Newtonsoft.Json
var jsonString = #"{""TargetId"":0,""ProductType"":null,""AssetId"":1239281845,""ProductId"":0,""Name"":""❤️🍀𝐒𝐀𝐋𝐄❗️🍀❤️ Red&Black Flannel + Backpack"",""Description"":""Shirt Image"",""AssetTypeId"":1,""Creator"":{""Id"":124026176,""Name"":""TheDestroyerPeter"",""CreatorType"":""User"",""CreatorTargetId"":124026176},""IconImageAssetId"":0,""Created"":""2017-12-12T19:48:24.693Z"",""Updated"":""2017-12-12T19:48:24.693Z"",""PriceInRobux"":null,""PriceInTickets"":null,""Sales"":0,""IsNew"":false,""IsForSale"":false,""IsPublicDomain"":false,""IsLimited"":false,""IsLimitedUnique"":false,""Remaining"":null,""MinimumMembershipLevel"":0,""ContentRatingTypeId"":0}";
var obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
Console.WriteLine(obj.Creator.Name); //"TheDestroyerPeter"
I'm using a program (Tiled) to create tilesets for a game, which spits out JSON files similar to this:
http://pastebin.com/t7UHzG7w
And i'm looking to turn it into data that I can use (a map of the "data", or an int[][] of the "data", as well as the width/height, all the other info is extraneous and I know already), and don't really know how to go about it.
How do I go from that JSON to data in a format I can deal with it?
You should use create a model class to represent the JSON data you have. Then you can use JavaScriptSerializer or Newsoft JOSN library to convert this data into object.
You should create model class for your data:
public class Layer
{
public List<int> data { get; set; }
public int height { get; set; }
public string name { get; set; }
public int opacity { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int width { get; set; }
public int x { get; set; }
public int y { get; set; }
}
public class Tileset
{
public int columns { get; set; }
public int firstgid { get; set; }
public string image { get; set; }
public int imageheight { get; set; }
public int imagewidth { get; set; }
public int margin { get; set; }
public string name { get; set; }
public int spacing { get; set; }
public int tilecount { get; set; }
public int tileheight { get; set; }
public int tilewidth { get; set; }
}
public class Data
{
public int height { get; set; }
public List<Layer> layers { get; set; }
public int nextobjectid { get; set; }
public string orientation { get; set; }
public string renderorder { get; set; }
public int tileheight { get; set; }
public List<Tileset> tilesets { get; set; }
public int tilewidth { get; set; }
public int version { get; set; }
public int width { get; set; }
}
Once this is done, you can use Newtonsoft.Json library to parse the string data into this object.
string text = "<Your Json data>";
var result = JsonConvert.DeserializeObject<Data>(text);
You can download Newtonsoft JSON library from here:
http://www.newtonsoft.com/json
Or use NPM as well:
Install-Package Newtonsoft.Json
Newtonsoft.Json is your friend.
https://www.nuget.org/packages/Newtonsoft.Json
http://www.newtonsoft.com/json/help/html/DeserializeObject.htm
You could use Newtonsoft.Json. Then after you declare your object / model, you could use it like
string Json = ""; // your Json string
var Result = JsonConvert.DeserializeObject<YourModel>(Json);
To get the Package, You can use the Nugget Function and type :
Install-Package Newtonsoft.Json
I using C# with Web API. I have this server method:
[System.Web.Http.HttpPost]
public HttpResponseMessage Test(CompareOutput model)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
This is my data-model:
public class CompareOutput
{
public CompareOutput(int wc, int source_start, int source_end, int suspected_start, int suspected_end)
{
this.WordsCount = wc;
this.SourceStartChar = source_start;
this.SourceEndChar = source_end;
this.SuspectedStartChar = suspected_start;
this.SuspectedEndChar = suspected_end;
}
public CompareOutput()
{
}
[JsonProperty("wc")]
public int WordsCount { get; set; }
[JsonProperty("SoS")]
public int SourceStartChar { get; set; }
[JsonProperty("SoE")]
public int SourceEndChar { get; set; }
[JsonProperty("SuS")]
public int SuspectedStartChar { get; set; }
[JsonProperty("SuE")]
public int SuspectedEndChar { get; set; }
public override string ToString()
{
return string.Format("{0} -> {1} | {2} -> {3}", this.SourceStartChar, this.SourceEndChar, this.SuspectedStartChar, this.SuspectedEndChar);
}
}
Now, I trying to call this method with this data (placed in BODY of the HTTP request):
{
"wc":11,
"SoS":366,
"SoE":429,
"SuS":393,
"SuE":456
}
This call isn't working. All the int members getting defualt values ("0").
When I trying to send this HTTP-BODY, it's working good:
{
"WordsCount":11,
"SourceStartChar":366,
"SourceEndChar":429,
"SuspectedStartChar":393,
"SuspectedEndChar":456
}
I can understand from that - alternative names (which defined in the model with JsonPropertry attribute) isn't working.
How can I solve it?
Thank you.
Try to decorate your properties this way...
[JsonProperty(PropertyName = "wc")]
public int WordsCount { get; set; }
[JsonProperty(PropertyName = "SoS")]
public int SourceStartChar { get; set; }
[JsonProperty(PropertyName = "SoE")]
public int SourceEndChar { get; set; }
[JsonProperty(PropertyName = "SuS")]
public int SuspectedStartChar { get; set; }
[JsonProperty(PropertyName = "SuE")]
public int SuspectedEndChar { get; set; }
I'm writing simple imageviewer for one imageboard. I'm using these 2 classes for navigation(navigation parameter for Frame.Navigate() method) in my app:
public class KonaParameter
{
public int page { get; set; }
public string tags { get; set; }
public KonaParameter()
{
page = 1;
}
}
public class Post
{
public int id { get; set; }
public string tags { get; set; }
public int created_at { get; set; }
public int creator_id { get; set; }
public string author { get; set; }
public int change { get; set; }
public string source { get; set; }
public int score { get; set; }
public string md5 { get; set; }
public int file_size { get; set; }
public string file_url { get; set; }
public bool is_shown_in_index { get; set; }
public string preview_url { get; set; }
public int preview_width { get; set; }
public int preview_height { get; set; }
public int actual_preview_width { get; set; }
public int actual_preview_height { get; set; }
public string sample_url { get; set; }
public int sample_width { get; set; }
public int sample_height { get; set; }
public int sample_file_size { get; set; }
public string jpeg_url { get; set; }
public int jpeg_width { get; set; }
public int jpeg_height { get; set; }
public int jpeg_file_size { get; set; }
public string rating { get; set; }
public bool has_children { get; set; }
public object parent_id { get; set; }
public string status { get; set; }
public int width { get; set; }
public int height { get; set; }
public bool is_held { get; set; }
public string frames_pending_string { get; set; }
public List<object> frames_pending { get; set; }
public string frames_string { get; set; }
public List<object> frames { get; set; }
public object flag_detail { get; set; }
}
The problem I faced is that suspending doesn't work. SuspensionManager throws "SuspensionManager failed" exception after await SuspensionManager.SaveAsync(); call(I googled that it's because of using complex types).
I tried to use string as a navigation parameter. It works, but I need more than 1 string for my parameter(List<string> doesn't work, I tried to use it).
How to suspend my app correctly?
The problem is that SuspensionManager uses Frame.GetNavigationState() to get the history of the Frame. It then tries to serialise the navigation history to a string, unfortunately it has no way to know how to serialise custom complex types as parameters like Post or KonaParameter and fails with an exception.
If you want to use SuspensionManager then you'll need to restrict yourself to simple types like int or string as parameters.
If you do need complex types then it's best to store them in some background service / repository with an identifier. You can then pass the identifier as the parameter.
The recommended approach is to serialize your "state" object, so it can be saved/restored as a string. You can use Json.NET to do this:
//save state
Post post = //...;
string state = JsonConvert.Serialize(post)
//restore state
Post post = JsonConvert.Deserialize<Post>(state);
If you want to use two objects (a Post and a KonaParameter), I'd consider creating an "aggregate" class that encapsulates both, and serializing/deserializing that instead:
public class State
{
public Post post {get; set;}
public KonaParameter param {get; set;}
}