asp.net-core-webapi

CORS header not being set for internal server error response ASP.NET Core 3.1

僤鯓⒐⒋嵵緔 提交于 2020-07-31 04:15:25
问题 Here is my CORS configuration: services.AddCors(options => { options.AddPolicy(name: "AllowedOrigins", policyBuilder => { var urls = Configuration.GetSection("Host:AllowedOrigins").Get<List<string>>(); policyBuilder.WithOrigins(urls.ToArray()) .AllowAnyMethod() .AllowAnyHeader() .SetIsOriginAllowed((host) => true) .AllowCredentials(); }); }); And in Configure method: app.UseRouting(); app.UseCors("AllowedOrigins"); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints =>

.net core injecting and resolving services

落花浮王杯 提交于 2020-07-20 17:06:58
问题 I am trying my hand at .net core web api. I have created a static method for registering my controllers like this: public static class RegistrationExtensions { public static void RegisterApplicationServices(this IServiceCollection services, IServiceProvider serviceProvider) { services.RegisterSingletons(); services.RegisterRequests(); services.RegisterTransient(serviceProvider); } public static void RegisterSingletons(this IServiceCollection services) { services.AddSingleton<Configuration>();

.net core injecting and resolving services

China☆狼群 提交于 2020-07-20 17:06:08
问题 I am trying my hand at .net core web api. I have created a static method for registering my controllers like this: public static class RegistrationExtensions { public static void RegisterApplicationServices(this IServiceCollection services, IServiceProvider serviceProvider) { services.RegisterSingletons(); services.RegisterRequests(); services.RegisterTransient(serviceProvider); } public static void RegisterSingletons(this IServiceCollection services) { services.AddSingleton<Configuration>();

Return Json on a bad request

自闭症网瘾萝莉.ら 提交于 2020-07-20 10:09:00
问题 So OK(value) returns formatted json with an application/json header. However the BadRequest() does not. If the request is a application/json shouldn't the response be like that even if it's a bad request? [HttpPost] public IActionResult Post([FromBody]Resolution value) { using (_ctx) { try { if (ValidateResolution(value.Size)) { _ctx.Resolution.Add(value); _ctx.SaveChanges(); return Ok(value); } return BadRequest("{ \"message\": \"hello\" }"); } catch (Exception) { return BadRequest(); } } }

Access HttpContextAccessor from startup.cs in .net Core WebApi

六眼飞鱼酱① 提交于 2020-07-18 07:21:58
问题 I'm logging exceptions to database in asp.net core. MyDbContext take HttpContextAccessor parameter.So, I'm sending HttpContextAccessor to MyDbContext.cs for access my JWT. But, I can't access my HttpContextAccessor from Startup.cs. How can I achieve this? Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddMvc(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddDbContext<MyDbContext>(); services

Access HttpContextAccessor from startup.cs in .net Core WebApi

荒凉一梦 提交于 2020-07-18 07:21:11
问题 I'm logging exceptions to database in asp.net core. MyDbContext take HttpContextAccessor parameter.So, I'm sending HttpContextAccessor to MyDbContext.cs for access my JWT. But, I can't access my HttpContextAccessor from Startup.cs. How can I achieve this? Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddMvc(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddDbContext<MyDbContext>(); services

Access HttpContextAccessor from startup.cs in .net Core WebApi

两盒软妹~` 提交于 2020-07-18 07:21:08
问题 I'm logging exceptions to database in asp.net core. MyDbContext take HttpContextAccessor parameter.So, I'm sending HttpContextAccessor to MyDbContext.cs for access my JWT. But, I can't access my HttpContextAccessor from Startup.cs. How can I achieve this? Startup.cs public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddMvc(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddDbContext<MyDbContext>(); services

Localization in external class libraries in ASP.NET Core

让人想犯罪 __ 提交于 2020-07-17 09:34:35
问题 I have two projects: MyWebApp - ASP.NET Core Web API MyServices - .NET Core class library, which contains helpful services for project above How can I add localization with IStringLocalizer to MyServices ? Where must be .resx files located? 回答1: This is how I solved it. Thanks to Popa Andrei answer for directing me to the right place. Class Library Solution -> right click -> Add -> New Project ... -> .Net standard -> Class Library -> I used the name ResourceLibrary ResourceLibrary |-

Asp.net core 2.0 Jwt doesn't ignore AllowAnonymous

故事扮演 提交于 2020-07-11 06:23:32
问题 I'm making a small new project using .net core 2.0 and jwt bearer authentication (https://github.com/aspnet/Security) Here is my Startup.cs /// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services"></param> public void ConfigureServices(IServiceCollection services) { // Add entity framework to services collection. var sqlConnection = Configuration.GetConnectionString("SqlServerConnectionString");

Is it possible to retrieve controller's action result from .NET Core middleware?

三世轮回 提交于 2020-07-09 12:05:38
问题 public class UsersController : APIControllerBase { public UsersController() { } public Client Get() { return new Client() { ClientID = 1, // LastUpdate = I want to update this field in middleware }; } public Client Get(int id) { return new Client() { ClientID = id // LastUpdate = I want to update this field in middleware }; } } public class SetClientLastUpdateMiddleware { private readonly RequestDelegate next; public SetClientLastUpdateMiddleware(RequestDelegate next) { this.next = next; }