asp.net-web-api

Correct way to post dates to Web API?

三世轮回 提交于 2020-01-15 06:56:07
问题 I am reading data from Web API and populate a form. When I submit it back to Web API, I get this error: {"Message":"An error has occurred.","ExceptionMessage":"Property 'StartDate' on type 'MvcApplication1.Models.ProductSale' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)].","ExceptionType":

POSTing from Angular to .net WebAPI is always null

若如初见. 提交于 2020-01-15 04:16:16
问题 I am trying to Post an object from AngularJS to a MVC 5 WebApi Controller, but the value is always null I can see in Chrome dev tools that the data can be found on the request. Angular Controller: $scope.join = function () { if (!$scope.joinForm.$valid) return; // Writing it to the server var payload = $scope.user; var res = $http.post('/api/some', JSON.stringify( { Data: { payload } }), { header: { 'Content-Type': 'application/json' } }); res.success(function (data, status, headers, config)

POSTing from Angular to .net WebAPI is always null

∥☆過路亽.° 提交于 2020-01-15 04:15:26
问题 I am trying to Post an object from AngularJS to a MVC 5 WebApi Controller, but the value is always null I can see in Chrome dev tools that the data can be found on the request. Angular Controller: $scope.join = function () { if (!$scope.joinForm.$valid) return; // Writing it to the server var payload = $scope.user; var res = $http.post('/api/some', JSON.stringify( { Data: { payload } }), { header: { 'Content-Type': 'application/json' } }); res.success(function (data, status, headers, config)

Multi thread and async at same time

我们两清 提交于 2020-01-15 03:50:10
问题 I have the following code: myObject object1 = null; Thread obj1Thread = new Thread(() => { object1 = _myService.GetMethod(variable1, variable2); }); obj1Thread.Start(); obj1Thread.Join(); myObject object2 = null; Thread obj2Thread = new Thread(() => { object2 = _myService.GetMethod2(variable3, variable4); }); obj2Thread.Start(); obj2Thread.Join(); As far as I understand, this code will create 2 new threads, run the specified methods, pause the main thread until both these threads complete,

How to update a collection in Web API?

江枫思渺然 提交于 2020-01-15 03:25:46
问题 I have a basic Order - OrderItem situation and I'm struggling how to update Items in one request (or if should I?) Let me explain and I'm gonna ask the question at the end. Models: public class Order { public int OrderId { get; set; } public string CustomerId { get; set; } public bool IsVoided { get; set; } public List<OrderItem> Items { get; set; } } public class OrderItem { public int OrderItemId { get; set; } public string Name { get; set; } public int Quantity { get; set; } public int

WebApi with Odata NextPage and Count not appearing in the JSON response

…衆ロ難τιáo~ 提交于 2020-01-15 03:17:32
问题 I have a webapi method that I want to switch oData paging etc on. I followed the example in http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options My method looks like: public PageResult<UserViewModel> GetUsers(ODataQueryOptions<UserViewModel> options) { var settings = new ODataQuerySettings() { PageSize = 2 }; var results = UserLogic.GetUsers(userId, UserManager, _db); var filtered = options.ApplyTo(results, settings); var pagedResult = new

How can I safely handle invalid requests for repository data?

眉间皱痕 提交于 2020-01-14 19:42:09
问题 With this code: public String Get(int id) { return platypi.Find(p => p.Id == id).Name; } ...I can get existing data via: http://localhost:33181/api/DPlatypus/N (where N corresponds to an existing ID). If I use a nonexistent value, though, it blows up. So, I tried this: public String Get(int id) { if (!string.IsNullOrEmpty(platypi.Find(p => p.Id == id).Name)) { return platypi.Find(p => p.Id == id).Name; } return string.Empty; } ...but it has no beneficial effect. Is there a way to safely

Simple Injector with dependency on two objects that implement same interface

醉酒当歌 提交于 2020-01-14 14:22:45
问题 I'm writing an application (exposed via an ASP.NET web API) that's entire purpose is to consume two data sources, and expose the similarities and differences. So the API has the following setup: public class FooController : WebAPI { public FooController(IFooRepository repoFromSourceA, IFooRepository repoFromSourceB) { ... } } Maintaining the distinction of which one is of which source (SourceA and SourceB cannot be interchanged) seems to make container.RegisterCollection(..) impossible (or

Simple Injector with dependency on two objects that implement same interface

只愿长相守 提交于 2020-01-14 14:22:07
问题 I'm writing an application (exposed via an ASP.NET web API) that's entire purpose is to consume two data sources, and expose the similarities and differences. So the API has the following setup: public class FooController : WebAPI { public FooController(IFooRepository repoFromSourceA, IFooRepository repoFromSourceB) { ... } } Maintaining the distinction of which one is of which source (SourceA and SourceB cannot be interchanged) seems to make container.RegisterCollection(..) impossible (or

Web API Basic Auth inside an MVC app with Identity Auth

自作多情 提交于 2020-01-14 14:12:14
问题 So I have a C# MVC app using Identity for its authentication. I now have a need to expose a few things via Web API to some of my clients. Instead of building a separate app, project, deployment... I've simply added an API Controller to my existing project. To keep things simple for my clients, I've decided to use Basic Auth , opting rather to force my clients into using SSL connections to my API. I've followed this very useful tutorial to implement the Basic Auth in my API: http://www