asp.net-core-webapi

Use appsettings to drive environment specific settings such as UseUrls

99封情书 提交于 2019-12-05 07:46:06
When I'm developing locally using VS Code, I'm going to use port 3000 because I'm a hipster. The non-hipsters want it on port 8080 on the server. That's cool, we got this. Microsoft docs give me the following example: public static void Main(string[] args) { var config = new ConfigurationBuilder() .AddJsonFile("hosting.json", optional: true) .AddCommandLine(args) .Build(); var host = new WebHostBuilder() .UseConfiguration(config) .UseKestrel() .Configure(app => { app.Run(async (context) => await context.Response.WriteAsync("Hi!")); }) .Build(); host.Run(); } I don't want to use hosting.json .

How to inject HttpHeader value in controller?

余生长醉 提交于 2019-12-05 07:35:48
I have Web API developed using ASP.NET Core API. Every incoming request has a custom header value inserted. eg x-correlationid . The controller use this value for logging and tracing the request. Currently I'm reading the value in each controller as below [Route("api/[controller]")] public class DocumentController : Controller { private ILogger<TransformController> _logger; private string _correlationid = null; public DocumentController(ILogger<DocumentController > logger) { _logger = logger; _correlationid = HttpContext.Request.Headers["x-correlationid"]; } [HttpPost] public async Task

HttpClient PostAsync posting null content

断了今生、忘了曾经 提交于 2019-12-05 07:32:51
I have one api calling another. and here is my code which seems to cause ModelState.IsValid = false on the other side of world. var baseUri = new Uri("http://localhost:5001/"): _httpClient.BaseAdress = baseUri; var data = new StringContent(content: model.Tostring(), encoding: Encoding.UTF8, mediaType: "application/json"); var response = await _httpClient.PostAsync("api/product", data); watching Post([FromBody]Product product) on the api being called I just see product=null . changing to Post([FromBody]object product) also shows null . calling the api from Postman works perfectly fine. which

.NET Core 2.2 Required Properties when object is not null. Complex Nested Model

北慕城南 提交于 2019-12-05 02:51:31
问题 In .NET Core I have a model that looks like this. public class Home { [Required] public int Number {get;set;} public Address Address {get;set;} // This is not required } public class Address { [Required] public string Street {get;set;} public IFormFile Picture {get;set;} } In a controller method that looks like this [HttpPost] public string AddHomes([FromForm]List<Home> homes) { if(!ModelState.IsValid) { return BadRequest(); } // Do some saving return Ok(); } In a form request that looks like

ASP.NET Core CreatedAtRoute No route matches the supplied values

走远了吗. 提交于 2019-12-05 02:22:46
Using ASP.NET Core 2.0.0 Web API, I'm trying to build a controller to do a database insert. The information can be inserted into the database just fine, but returning a CreatedAtRoute throws an 'InvalidOperationException: No route matches the supplied values.' Everything I've found online so far says this was a bug with early pre-release versions of ASP.NET Core and has since been fixed, but I'm not really sure what to do about this. The following is my controller code: [Produces("application/json")] [Route("api/page")] public class PageController : Controller { private IPageDataAccess

How to validate AWS Cognito JWT in .NET Core Web API using .AddJwtBearer()

China☆狼群 提交于 2019-12-05 01:49:10
问题 I was having some trouble figuring out how to go about validating a JWT given to the client by AWS Cognito inside my .NET Core Web API. Not only could I not figure out what the variables for Microsoft.IdentityModel.Tokens.TokenValidationParameters were supposed to be, but once I finally did, I didn't know how to retrieve the JWT key set from https://cognito-idp.{region}.amazonaws.com/{pool ID}/.well-known/jwks.json Finally, though a lot of random Googling and trial and error, I found a

Alternative to Server.Transfer in ASP.NET Core

时间秒杀一切 提交于 2019-12-04 21:19:28
问题 I am migrating an ASP.NET application to ASP.NET Core and they have some calls to HttpServerUtility.Transfer(string path). However, HttpServerUtility does not exist in ASP.NET Core. Is there an alternative that I can use? Or is Response.Redirect the only option I have? I want to maintain the same behaviour as the old application as much as possible since there is a difference in between Server.Transfer and Response.Redirect. 回答1: I see some options for you, depending on your case: Returning

ASP.NET Core WebAPI Security Considerations

烈酒焚心 提交于 2019-12-04 18:47:40
问题 My WebAPI is, just that, an API backend for my UI to consume. In fact, I will have perhaps 10's of WebAPI services that my UI will use. I'm having difficulty understanding what I need to consider in terms of security. My APIs are secured using Bearer tokens, and allow https only. I have CORS set up and they only allow the origin https://my-front.end This all works great. However.. how can I protect against C/XSRF and replay attacks on a WebAPI? Do I even need to? Setting up anti-CSRF is

Version Conflict detected for Microsoft.EntityFrameworkCore.Install

谁都会走 提交于 2019-12-04 16:48:43
问题 I am getting the following error when adding an asp.net core api to an existing project I am working on. "Version Conflict detected for Microsoft.EntityFrameworkCore.Install/reference Microsoft.EntityFrameworkCore 2.2.1 directly to project to resolve this issue" I tried to add the Nuget package but get further version conflicts across a number of different packages and the process always fails. At first I thought this may be an issue with my project so I started a new solution from scratch

How to use ASP.NET Core MVC with Angular using Identity Server 4 to connect to web API

走远了吗. 提交于 2019-12-04 14:32:07
I am writing after the response recieved from the post here -------------------------------------- I am in the process of developing an application that has an MVC core app that simply loads the angular application. The angular application will then connect to a Web API to perform CRUD operations. Im aware its possible to use cookie authentication in conjuction with odic hybrid flow to generate a cookie, but not sure how the angular app can get a reference to the access token and renew it when it expires in order to connect to my web api. I read through this article https://damienbod.com/2017