asp.net-core-webapi

404 error when making a POST call from Angular HttpClient to ASP.NET Core 2.2 Web API (with CORS enabled)

蓝咒 提交于 2019-12-02 08:14:32
I've written a simple ASP.NET Core 2.2 Web API . The POST method always returns a 404 , but GET requests succeed. public class TestPayload { public string test1 { get; set; } public string test2 { get; set; } } [Route("api/[controller]")] [ApiController] public class TestController: ControllerBase { // POST api/create [HttpPost] public async Task<ActionResult<string>> Create([FromBody] TestPayload value) { return Ok(""); } } I get back a 404 error in my Angular HttpClient front-end. let headers = new HttpHeaders().set('Content-Type', 'application/json'); return this.http.post<any>(`${config

Deleting PrecompiledViews.dll from ASP.Net Core 2 API

我的未来我决定 提交于 2019-12-02 08:03:21
问题 In .NET Core 2 Web API app, Publish to folder feature in MS VS 2017 produce: <ProjectAssembly>.PrecompiledViews.dll <ProjectAssembly>.PrecompiledViews.pdb Offical docs says that PrecompiledViews related to precompiling Razor Views , but my API doesn't contain any views or static files, just REST endpoints that return json. Using .Net reflector I found the PrecompiledViews.dll empty . So I deleted PrecompiledViews.dll and tested my API and it seems to work fine without any warnings or

Deleting PrecompiledViews.dll from ASP.Net Core 2 API

▼魔方 西西 提交于 2019-12-02 05:57:16
In .NET Core 2 Web API app, Publish to folder feature in MS VS 2017 produce: <ProjectAssembly>.PrecompiledViews.dll <ProjectAssembly>.PrecompiledViews.pdb Offical docs says that PrecompiledViews related to precompiling Razor Views , but my API doesn't contain any views or static files, just REST endpoints that return json. Using .Net reflector I found the PrecompiledViews.dll empty . So I deleted PrecompiledViews.dll and tested my API and it seems to work fine without any warnings or exceptions. Is it safe to delete PrecompiledViews.dll and pdp if the API not using any razor views? If yes, Is

How do I unit test model validation in controllers decorated with [ApiController]?

痞子三分冷 提交于 2019-12-02 04:41:31
问题 As pointed out in this anwer to Asp.Net Core 2.1 ApiController does not automatically validate model under unit test, the automatic ModelState validation that ASP.NET Core 2.1's ApiControllerAttribute gives us only works when actualyy requestion the action at runtime, not by calling it with an invalid parameter in a unit test. However, I still want to test if my action actually returns a BadRequestResult when supplying an incorrect model. Is there any way of doing this? I get that I can still

Asp.net core 2.2 ModelBinder Unit Test Issues

北慕城南 提交于 2019-12-02 04:34:37
I'm seeing odd behavior while attempting to debug xUnits against components of the asp.net core pipeline. The code posted below has all purposeful functionality stripped out to illustrate the problem only which is : Not hitting all my breakpoints in JsonModelBinder. Not exiting on "return Task.Completed" even though it's being evaluated. The production code for JsonModelBinder contains more logic to deserialize the incoming string data. This code contains failure logic which contains a number of return Task.Completed statements. When using this code the the debugger will evaluate these return

How to throwing 404 on bad .NET Core API route?

假如想象 提交于 2019-12-02 02:09:16
问题 I have a .NET Core web app which has an API. I've defined an Middleware class based on this answer like so: public class ErrorHandlingMiddleware { private readonly RequestDelegate next; private readonly ILogger logger; public ErrorHandlingMiddleware(RequestDelegate next, ILoggerFactory loggerFactory) { this.next = next; logger = loggerFactory.CreateLogger<ErrorHandlingMiddleware>(); } public async Task Invoke(HttpContext context) { try { await next(context); } catch (Exception ex) { logger

IFormFile always return null in asp.net core 2.1

[亡魂溺海] 提交于 2019-12-02 00:55:21
问题 Api method is looks like below [HttpPost] public async Task<BaseListResponse<MediaStorageModel>> MediaBrand(IFormFile file, int brandId) { var files = new List<IFormFile>(); files.Add(file); var response = await this.Upload(files, "brand", brandId); return response; } My postman configuration Upgrade my dotnet core from 2.0 to 2.1 thie become not working, can anyone help about this. What going wrong 回答1: I've faced the same issue, I was able to fix it by applying the 'Name' named parameter to

ASP.Net Identity built in functions with custom tables in ASP.Net Core

北城以北 提交于 2019-12-02 00:16:22
I am using ASP.Net Core Web Api 2 on .Net 2.1 Framework I have custom AppUsers and AppRoles tables, linked with bridge table AppUserRoles My main problem is that I want to use [Authorize(Roles = "UserRole")] As User.Identity is working fine and I am getting user Id from User.Identity.Name I thought there was some way to set roles and check them before controller request, or to use User.IsInRole("UserRole") for checking inside controller. Is it possible to rebuild or overload .IsInRole("UserRole") function or [Authorize(Roles = "UserRole")] attribute background function somehow, so I could

How do I unit test model validation in controllers decorated with [ApiController]?

我的未来我决定 提交于 2019-12-01 22:52:07
As pointed out in this anwer to Asp.Net Core 2.1 ApiController does not automatically validate model under unit test , the automatic ModelState validation that ASP.NET Core 2.1's ApiControllerAttribute gives us only works when actualyy requestion the action at runtime, not by calling it with an invalid parameter in a unit test. However, I still want to test if my action actually returns a BadRequestResult when supplying an incorrect model. Is there any way of doing this? I get that I can still manually check if ModelState.IsValid is false, and returning BadRequest() myself, but that kind of

Reloading Options with reloadOnChange in ASP.NET Core

十年热恋 提交于 2019-12-01 21:29:56
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(IServiceCollection services) { services.Configure<AppSettings>(Configuration); //... } In a singleton