asp.net-core-webapi

ASP.NET Core WebAPI default route not working

佐手、 提交于 2019-11-28 03:08:54
问题 I've followed several examples suggesting that to set my default route in an ASP.NET Core WebAPI project, I need to replace app.UseMvc(); with app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action}", defaults: new { controller = "Traders", action = "Get" }); }); But when I run it defaults to localhost:54321/api/values and it should default to localhost:54321/Traders What's wrong? 回答1: As @tmg mentioned, do the following: Right click your web project ->

Reading appsettings.json from .net standard library

不想你离开。 提交于 2019-11-28 01:16:43
问题 i have started a new RESTful project using .NET Core Framework. I divided my solution in two parts: Framework (set of .NET standard libraries) and Web (RESTful project). With Framework folder i provide some of library for furthers web project and into one of these i'd like to provide a Configuration class with the generic method T GetAppSetting<T>(string Key) . My question is: how can i get the access to the AppSettings.json file in .NET Standard? I have found so many example about reading

ASP.NET Core API JSON serializersettings per request

自作多情 提交于 2019-11-28 01:07:09
Based on some value in the request (header or in the url) I want to change the serialization of my DTO objects. Why? Well I've applied the [JsonProperty("A")] to my DTO's but depending on the client (website or mobile app) it want to use that property or not. I started with services .AddMvc() .AddJsonOptions(opt => { #if DEBUG opt.SerializerSettings.ContractResolver = new NoJsonPropertyNameContractResolver(); #endif } So while debugging I get JSON with full propertynames. I use the JsonProperty attribute to shorten the response JSON, which works fine with the mobile app (Xamarin) which

Using a C# 7 tuple in an ASP.NET Core Web API Controller

旧城冷巷雨未停 提交于 2019-11-28 01:00:31
Do you know why this works: public struct UserNameAndPassword { public string username; public string password; } [HttpPost] public IActionResult Create([FromBody]UserNameAndPassword usernameAndPassword) { Console.WriteLine(usernameAndPassword); if (this.AuthenticationService.IsValidUserAndPasswordCombination(usernameAndPassword.username, usernameAndPassword.password)) return new ObjectResult(GenerateToken(usernameAndPassword.username)); return BadRequest(); } But when I replace it with a tuple, this doesn’t work? [HttpPost] public IActionResult Create([FromBody](string username, string

How to stop self-referencing loop in .Net Core Web API?

此生再无相见时 提交于 2019-11-28 00:46:25
I'm having some issues which I'm guessing are related to self-referencing using .NET Core Web API and Entity Framework Core. My Web API starting choking when I added .Includes for some navigation properties. I found what appears to be a solution in the older Web API but I don't know how to implement the same thing for .NET Core Web API (I'm still in the early learning stages). The older solution was sticking this in the Application_Start() of the Global.asax: GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize; I

How to configure concurrency in .NET Core Web API?

淺唱寂寞╮ 提交于 2019-11-28 00:03:39
In the old WCF days, you had control over service concurrency via MaxConcurrentCalls setting. MaxConcurrentCalls defaulted to 16 concurrent calls but you could raise or lower that value based upon your needs. How do you control server side concurrency in .NET Core Web API? We probably need to limit it in our case as too many concurrent requests can impede overall server performance. ASP.NET Core application concurrency is handled by its web server . For example: Kestrel var host = new WebHostBuilder() .UseKestrel(options => options.ThreadCount = 8) It is not recommended to set Kestrel thread

Read request body twice

半腔热情 提交于 2019-11-27 23:36:28
I am trying to read the body in a middleware for authentication purposes, but when the request gets to the api controller the object is empty as the body has already been read. Is there anyway around this. I am reading the body like this in my middleware. var buffer = new byte[ Convert.ToInt32( context.Request.ContentLength ) ]; await context.Request.Body.ReadAsync( buffer, 0, buffer.Length ); var body = Encoding.UTF8.GetString( buffer ); Pinpoint If you're using application/x-www-form-urlencoded or multipart/form-data , you can safely call context.Request.ReadFormAsync() multiple times as it

Angular4 ASP.NET Core 1.2 Windows Authentication CORS for PUT and POST Gives 401

依然范特西╮ 提交于 2019-11-27 22:28:29
My IDE is Visual Studio 2017. I've got an Angular4 client talking to a WebAPI backend in Core, and CORS is working as configured EXCEPT for the PUT and POST methods. The GET method is subject to the same preflight OPTIONS method in Chrome that the PUT and POST methods are, but GET is working fine. It appears that the IIS Express server in Visual Studio is not forwarding the requests to the Kestrel server. Both Methods work in Postman, but not when Angular4 makes the call. Here's the code: Angular4 POST post(api: string, object: any): Observable<any> { let body = JSON.stringify(object); let

Routing is not working with self-hosted web API [duplicate]

江枫思渺然 提交于 2019-11-27 22:27:42
This question already has an answer here: WebApi giving 404 whilst debugging; works when published 1 answer This is essentially what I have, a very simple set of three files with fresh asp.net core 2.1 (actually copy-pasted from tutorials): public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); } Then goes the simplest startup class Startup { public Startup(IConfiguration configuration) { Configuration =

Asp.Net Core Web API app: how to change listening address?

我的梦境 提交于 2019-11-27 21:53:03
问题 I write simple Asp.Net Core WebAPI 2.0 application, it works on my local machine. But i want to deploy it to server. So, i do it. My system: Ubuntu 16.04. Asp.Net WebAPI 2.0 (and dotnet version 2.1.105) But, when app starts it writes: Now listening on:http://localhost:53115 When i try to get values from it: http://id_address:53115/api/values And i can not get response. In Postman: Could not get any response There was an error connecting to http://id_address:53115/api/values. Why this might