asp.net-web-api2

Web Api 2: [Required] for value types?

别等时光非礼了梦想. 提交于 2019-12-10 19:31:04
问题 Using the [Required] data annotation in Web Api input models only seems to check for reference types being instantiated to null: public class MyInputModel { [Required] // This works! ModelState fails. public CustomClass MyCustomProperty { get; set; } } How can we get this to work with value types WITHOUT the default instantiation? public class MyInputModel { [Required] // This is ignored because MyDouble is defaulted to 0 public double MyDouble { get; set; } } Is the only way through using

CORS works for access token but not for refresh token in Web Api 2

这一生的挚爱 提交于 2019-12-10 19:17:01
问题 I have a web api 2 app which I call to using an angularjs client. The web api app is capable of issuing access tokens and refresh tokens for authentication. Having the following lines in the "GrantResourceOwnersCredentials" method, the CORS is working fine for allowing to issue access tokens: var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin"); if (allowedOrigin == null) allowedOrigin = "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[

POST to a related collection in WEB API 2 with OData 4

我们两清 提交于 2019-12-10 17:22:39
问题 I want to have a route like: /Accounts(id)/Orders where I can execute a POST to create an order. I can't find a way to add this route using OData in WebApi. For GET there is a convention to follow to get related collections, but I am not able to find any convention for posting new entities to a related collection. Is there a standard way to handle this POST request with Web API 2 and OData 4 ? 回答1: Added the following attributes to the method and it worked: [HttpPost] [ODataRoute("Accounts(

Does Asp.net Web API 2.2 OData4 support group by clause?

核能气质少年 提交于 2019-12-10 16:51:34
问题 Does Asp.net Web API 2.2 OData4 support aggregates and groupby clause? I could not find any conclusive answer to this . 回答1: An alternative is to implement your service using the QueryByCube linq extension method provided by the AdaptiveLINQ component. For example: [EnableQuery] public IQueryable<SalesCubeItem> Get() { return DataContext.OrderDetails.QueryByCube(new SalesCubeDefinition()); } where SalesCubeDefinition define: 2 dimensions: ProductName and CustomerName 2 measures: Sales ,

WebAPI 2 Attribute routing with areas not working

主宰稳场 提交于 2019-12-10 16:47:36
问题 I'm having trouble getting WEBAPI 2 attribute routing to work. The routing scheme I'm looking for is /api/{product}/{controller}/{id-optional} , so like /api/Vision/IdCard . The controllers are in an area and are set up like this: namespace DataServices.Controllers.Vision { [RoutePrefix("api/vision")] public class IdCardController : BaseApiController { [System.Web.Mvc.RequireHttps] [Route("idcard")] public IdCardViewModel Get(HttpRequestMessage request) {...} Whenever I do a get to this, I

WebAPI Response never completes when returning HttpResponseMessage and Application Insights is configured

倾然丶 夕夏残阳落幕 提交于 2019-12-10 16:13:22
问题 I have an MVC5/WebAPI2 application which has had Application Insights enabled since I created the web project. WebApi methods that return objects (e.g. string, model objects) are returned as expected - serialized into JSON or XML. public class TestController : ApiController { [HttpGet] [AllowAnonymous] async public Task<HttpResponseMessage> ReadString(int id) { HttpResponseMessage response = Request.CreateResponse(); string str; using (HttpClient client = new HttpClient()) { Uri uri = new Uri

Deny HTML in WebAPI model properties

断了今生、忘了曾经 提交于 2019-12-10 16:12:46
问题 I've been searching for this for a while, and couldn't find anything, apologies if there is something and I've missed it. Is there anything in the Web-API 2 framework that will automatically reject HTML tags on string model properties (except for a select few)? I know I could create a custom validation attribute, and whack it on every property, but that's a lot of over head to maintain and remember to do on all new models. public class Person { public string Name{ get; set; } // ... snipped }

HttpContext .NET core saving instance in Middleware

核能气质少年 提交于 2019-12-10 15:43:12
问题 Is it safe to store an instance of HttpContext in a middleware? Example: public class TestMiddleware { private readonly RequestDelegate next; private HttpContext context; public TestMiddleware(RequestDelegate next) { this.next = next; } public async Task Invoke(HttpContext context) { try { this.context = context; I would like to use it in other private methods to work on it, so I can either pass it around as parameter to those function or use it as shown in the example. But is it thread safe?

How to get the stream for a Multipart file in webapi upload?

自闭症网瘾萝莉.ら 提交于 2019-12-10 15:05:26
问题 I need to upload a file using Stream (Azure Blobstorage), and just cannot find out how to get the stream from the object itself. See code below. I'm new to the WebAPI and have used some examples. I'm getting the files and filedata, but it's not correct type for my methods to upload it. Therefore, I need to get or convert it into a normal Stream, which seems a bit hard at the moment :) I know I need to use ReadAsStreamAsync().Result in some way, but it crashes in the foreach loop since I'm

How can I confirm if an async EF6 await db.SaveChangesAsync() worked as expected?

ぐ巨炮叔叔 提交于 2019-12-10 14:32:39
问题 My code looks like this: public async Task<IHttpActionResult> Delete(int id) { var userId = Int32.Parse(User.Identity.GetUserId()); UserTest userTest = await db.UserTests.FindAsync(id); if (userTest == null) { return NotFound(); } if (userTest.UserId != userId) { return Unauthorized(); } db.UserTests.Remove(userTest); await db.SaveChangesAsync(); return Ok(); } I think everything up to the db.SaveChangesAsync is okay but how can I confirm if the db.SaveChangesAsync works before doing a return