Hourly, Daily, Monthly Helper+Model methods

You can create daily,monthly and yearly data by using the same logic. You just have to replace Hour with Day,Month or Year. List<dynamic> dailyData = new List<dynamic>(); DateTime dayIterator = StartDate.Value; while (dayIterator.Hour <= ddvm.CurrentDay) { if (ddvm.DailyPaymentTotal != null && ddvm.DailyPaymentTotal.Count() > 0 ) { DailyPaymentTotalModel day = ddvm.DailyPaymentTotal.FirstOrDefault(hpt => hpt.DateTimeStamp == dayIterator); if … Read more

Why can’t we add a Web API as a “service reference” in Visual Studio the same way we can with WCF or ASMX?

Do you mean a Rest Web Service? With Rest, there is no service definition page, like with WCF or ASMX. Usually people want to use a Rest API with JSON.. however.. if you are just looking for a JSON output, and you want your clients to quickly be able to connect to your service, you … Read more

How to auto log every request in .NET Core WebAPI?

ActionFilter will work until you need to log only requests processed by MVC middleware (as controller actions). If you need logging for all incoming requests, then you need to use a middleware approach. Good visual explanation: Note that middleware order is important, and if your logging should be done at the start of pipeline execution, … Read more

Why does adding a dependency in my Web API (ASP.NET v5) project not work fully?

When you build, check out the “Project” column – it notes that the build that is failing is “ASP.NET Core 5.0” (not “ASP.NET 5.0”). In the upper left dropdown of the code editor, you can choose different views – if you select the “ASP.NET Core 5.0” one, you’ll see that the NodaTime namespace is undefined. … Read more

Which HTTP status code to return when the DELETE operation is not allowed for particular reason

I would go with 409: Conflict, because what you have is a violation of resource state. 405: Method Not Allowed would also work. If you’d want to use a 405, you’d have to send an Allow header to indicate the supported methods, and the supported methods would vary depeding on the resource’s state. In my … Read more

Calling external HTTP service using HttpClient from a Web API Action

Aha, I needed to do the following (return a Task rather then void): // GET api/values public async Task<IEnumerable<string>> Get() { var result = await GetExternalResponse(); return new string[] { result, “value2” }; } private async Task<string> GetExternalResponse() { var client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(_address); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadAsStringAsync(); … Read more

how to post arbitrary json object to webapi

You can have your post method that takes in a HttpRequestMessage to by pass the model binding logic and you can read the content of the request directly: public HttpResponseMessage Post(HttpRequestMessage req) { var data = req.Content.ReadAsStringAsync().Result; // using .Result here for simplicity… … } By the way, the reason why the action that takes … Read more