asp.net-core-webapi

.Net Core OData enable filtering for single entity set

*爱你&永不变心* 提交于 2020-01-15 15:27:07
问题 I'm running the Beta of OData on .Net Core. I'm trying to only enable certain features of odata depending on the controller. My startup class looks like so: services.AddOData(); //... app.UseMvc(routeBuilder => { //routeBuilder.Count().Filter().OrderBy().Expand().Select().MaxTop(null); routeBuilder.MapODataServiceRoute("odata", null, GetModel()); routeBuilder.EnableDependencyInjection(); }); public static IEdmModel GetModel() { var builder = new ODataConventionModelBuilder(); var skillSet =

JWT Authentication by role claims in ASP.NET core Identity

给你一囗甜甜゛ 提交于 2020-01-15 09:41:29
问题 How I can authenticate user by claims, which contains in user roles? In Startup.cs : services.AddAuthorization(options => { options.AddPolicy("CanEdit", policy => policy.RequireClaim("CanEdit")); }); And in login controller I have: private async ValueTask<JwtSecurityToken> GetJwtSecurityToken(ApplicationUser user){ //var totalClaims = new List<Claim>(); //var userRoles = await _userManager.GetRolesAsync(user); //foreach (var role in userRoles) { // var roleClaims = await _roleManager

DataMember Attribute is not honored in dotnet core 3.0

南笙酒味 提交于 2020-01-15 06:51:30
问题 I created a sample dotnet core 3.0 Web API project and did the following changes, Create a Model class TestData using System.Runtime.Serialization; namespace WebApplication17.Models { [DataContract] public class TestData { [DataMember(Name = "testaction")] public string Action { get; set; } } } Then I made changes in controller WeatherForecastController , to add a post endpoint [HttpPost("package/{packageName}/version/{version}")] public void Post(string packageName, string version, [FromBody

Generic ActionResult return type for API Controller

℡╲_俬逩灬. 提交于 2020-01-15 04:45:27
问题 I have the following use case for the GetProducts() method. Good path: Returns array of type product: Product[] Bad path: Returns status code of 500 and descriptive string error message. ( <?Type?> below is my own markup for the purposes of this post) [Route("api/[controller]/[action]")] [ApiController] public class ProductsController : ControllerBase { [HttpGet] public ActionResult<?Type?> GetProducts() { try { Product[] products = DataAccess.GetProductsFromDb(); return products; } catch {

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

asp.net core build error : 'HttpRequestMessageExtensions'

核能气质少年 提交于 2020-01-15 03:06:10
问题 Using ASPNet Core 1.1 Web API template and trying to create a response on Post. Getting the below error while building the project. Thanks for your help ! Controllers\MessagesController.cs(37,28,37,72): error CS0433: The type 'HttpRequestMessageExtensions' exists in both 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 回答1: You should remove reference to

asp.net core build error : 'HttpRequestMessageExtensions'

[亡魂溺海] 提交于 2020-01-15 03:04:08
问题 Using ASPNet Core 1.1 Web API template and trying to create a response on Post. Getting the below error while building the project. Thanks for your help ! Controllers\MessagesController.cs(37,28,37,72): error CS0433: The type 'HttpRequestMessageExtensions' exists in both 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' and 'System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 回答1: You should remove reference to

Asp.net Core model binder accepts random integer for boolean types

淺唱寂寞╮ 提交于 2020-01-15 01:24:33
问题 Given the model has a boolean property: public class Person { public string Name { get; set; } public bool IsMale { get; set; } } When trying to POST the following payload: { "name": "Bob", "isMale": 12345 // any random integer } To a simple action: [HttpPost] public IActionResult Post([FromBody] Person person) { if (ModelState.IsValid) return Ok(); return BadRequest(ModelState); } The person.IsMale property gets the value of true . If passing isMale: "foobar" i get an invalid type error If

Asp.net Core model binder accepts random integer for boolean types

末鹿安然 提交于 2020-01-15 01:24:05
问题 Given the model has a boolean property: public class Person { public string Name { get; set; } public bool IsMale { get; set; } } When trying to POST the following payload: { "name": "Bob", "isMale": 12345 // any random integer } To a simple action: [HttpPost] public IActionResult Post([FromBody] Person person) { if (ModelState.IsValid) return Ok(); return BadRequest(ModelState); } The person.IsMale property gets the value of true . If passing isMale: "foobar" i get an invalid type error If

Could not load file or assembly 'System.Runtime.Loader' for adding application parts into MVC

只愿长相守 提交于 2020-01-14 08:00:54
问题 I'm trying to load application parts into an ASP.NET Core 2.0 MVC appication by using the System.Runtime.Loader package but it won't let me. The error message says: FileNotFoundException: Could not load file or assembly 'System.Runtime.Loader, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. In the Startup.cs I use it like this (it should just be proof of concept that this works so I can move it into a