asp.net-core-webapi

Using inherited classes in .NET web API POST/PUT method

北战南征 提交于 2019-12-20 04:38:10
问题 I can't figure out how I can work with inherited classes in an web API controller. I have to create only one API controller to create and update inherited objects in the database. Similar to my models (to all these models exists a Dto): public class Animal { public virtual string Name {get; set;} // e.g. Harry public virtual string Type {get; set;} // e.g. Dog } public class AnimalDto { public string Name; public Type Type; } public class Dog : Animal { public virtual bool CanBark {get; set;}

Reloading Options with reloadOnChange in ASP.NET Core

别来无恙 提交于 2019-12-20 02:27:40
问题 In my ASP.NET Core application I bind the appsettings.json to a strongly typed class AppSettings . public Startup(IHostingEnvironment environment) { var builder = new ConfigurationBuilder() .SetBasePath(environment.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public void ConfigureServices

Enum type no longer working in .Net core 3.0 FromBody request object

二次信任 提交于 2019-12-19 07:54:35
问题 I have recently upgraded my web api from .Net core 2.2 to .Net core 3.0 and noticed that my requests are getting an error now when I pass an enum in a post to my endpoint. For example: I have the following model for my api endpoint: public class SendFeedbackRequest { public FeedbackType Type { get; set; } public string Message { get; set; } } Where the FeedbackType looks like so: public enum FeedbackType { Comment, Question } And this is the controller method: [HttpPost] public async Task

Angular JS MVC Web API Model/ Parameter not binding .NET Core

隐身守侯 提交于 2019-12-19 07:27:06
问题 I am using Angular JS with TypeScript and ASP.NET Core MVC/API. I have an apiService which deals with all POST and GET requests to the server, which looks like this: module TBApp { export class apiService { static $inject = ['$http', 'notificationService']; constructor(private $http, private notificationService: notificationService) { } get(url, config, success, failure) { return this.$http.get(url, config) .then(result => { this.handleResponse(result, success); }, result => { this

Accept x-www-form-urlencoded in Web API .Net Core

孤街醉人 提交于 2019-12-18 21:52:38
问题 I have a .Net Core Web API that is returning a 415 Unsupported Media Error when I try to post some data to it that includes some json. Here's part of what is returned in the Chrome Debugger: Request URL:http://localhost:51608/api/trackAllInOne/set Request Method:POST Status Code:415 Unsupported Media Type Accept:text/javascript, text/html, application/xml, text/xml, */* Content-Type:application/x-www-form-urlencoded action:finish currentSco:CSharp-SSLA:__How_It_Works_SCO data:{"status":

ASP.NET Core: [FromQuery] usage and URL format

心不动则不痛 提交于 2019-12-18 19:19:17
问题 I am trying to use [FromQuery] in my web api and I am not sure how to use it. Here's the GetAllBooks() method in the controller: [HttpGet] [Route("api/v1/ShelfID/{shelfID}/BookCollection")] public async Task<IActionResult> GetAllBooks(string shelfID, [FromQuery] Book bookinfo) { //do something } Here's the Book model class: public class Book { public string ID{ get; set; } public string Name{ get; set; } public string Author { get; set; } public string PublishDate { get; set; } } I am

Read JSON post data in ASP.Net Core MVC

有些话、适合烂在心里 提交于 2019-12-18 19:17:08
问题 I've tried to find a solution for this, but all the ones coming up are for previous versions of ASP.Net. I'm working with the JWT authentication middleware and have the following method: private async Task GenerateToken(HttpContext context) { var username = context.Request.Form["username"]; var password = context.Request.Form["password"]; //Remainder of login code } This gets the sent data as if it was form data, but my Angular 2 front end is sending the data as JSON. login(username: string,

The configuration file 'appsettings.json' was not found and is not optional

心已入冬 提交于 2019-12-18 03:02:23
问题 The Azure error is: .Net Core: Application startup exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. So this is a bit vague. I can't seem to nail this down. I'm trying to deploy a .Net Core Web API project to Azure, and I'm getting this error: :( Oops. 500 Internal Server Error An error occurred while starting the application. I've deployed plain old .Net WebAPI's and they have worked. I've followed online tutorials and

ASP.NET Core with EF Core - DTO Collection mapping

佐手、 提交于 2019-12-17 22:43:02
问题 I am trying to use (POST/PUT) a DTO object with a collection of child objects from JavaScript to an ASP.NET Core (Web API) with an EF Core context as my data source. The main DTO class is something like this ( simplified of course ): public class CustomerDto { public int Id { get;set } ... public IList<PersonDto> SomePersons { get; set; } ... } What I don't really know is how to map this to the Customer entity class in a way that does not include a lot of code just for finding out which

Read request body twice

廉价感情. 提交于 2019-12-17 16:34:20
问题 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 ); 回答1: If you're using application/x-www-form-urlencoded