asp.net-core-webapi

Porting HttpModule .Net Class Library to .Net Core Web API

左心房为你撑大大i 提交于 2019-12-11 02:25:53
问题 I am migrating a project from .net Web Application to .Net core Web API. I am using HTTP Module which is .net framework class library, in IIS Integrated mode. So, thought to port as it is, to my new core app. I pasted web.config to my new core app and added a project reference to that Http Module and it started working. I am having some Context.Response.Write in my BeginRequest method, and I can see those lines whenever I am making a call to my web api. But, I am having some questions here.

Integration Testing with AutoMapper fails to initialise configuration

狂风中的少年 提交于 2019-12-11 02:21:06
问题 Frameworks & Packages .NETCoreApp 1.1 Xunit 2.2.0 AutoMapper 6.0.2 Microsoft.AspNetCore.TestHost 1.1.1 Microsoft.NET.Test.Sdk 15.0.0 Integration Test public class ControllerRequestsShould { private readonly TestServer _server; private readonly HttpClient _client; public ControllerRequestsShould() { _server = new TestServer(new WebHostBuilder() .UseContentRoot(Constants.apiProjectRoot) .UseStartup<Startup>() .UseEnvironment(Constants.testingEnvironment)); _client = _server.CreateClient();

How to add AzureAD AND AzureADBearer to asp.net core 2.2 web api

梦想的初衷 提交于 2019-12-10 21:51:35
问题 I'm trying to author a website that uses AzureAD to authenticate users to access UIs to author items in a DB. And I also want this API to be callable by other services via a bearer token. services.AddAuthentication(o => { o.DefaultScheme = AzureADDefaults.BearerAuthenticationScheme; o.DefaultAuthenticateScheme = AzureADDefaults.AuthenticationScheme; }) .AddAzureAD(options => Configuration.Bind("AzureAd", options)) .AddAzureADBearer(options => Configuration.Bind("AzureAd", options)); I want

DelegateHandler from ASP.NET to .NET Core

心不动则不痛 提交于 2019-12-10 19:04:02
问题 In an old asp.net project I have a class that implements DelegatingHandler that I set to each route: public class AdminSecurityMessageHandler : DelegatingHandler { private readonly HttpConfiguration _config; public AdminSecurityMessageHandler(HttpConfiguration config) { if (config == null) { throw new ArgumentNullException("config"); } _config = config; } protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { var repository =

How to remove extra spaces in input model in dot net core?

孤街醉人 提交于 2019-12-10 18:28:38
问题 I have found a link to remove extra spaces in the model properties which is string type How to trim spaces of model in ASP.NET MVC Web API How to achieve the same functionality in dot net core 2.1 web api? Or is there any build in formatters available in dotnet core for removing extra spaces in input and output model? Thanks in advance? 回答1: I believe the answer you linked is probably your best option. So create a converter as per the anwser. class TrimmingConverter : JsonConverter { public

Enum as Required Field in ASP.NET Core WebAPI

為{幸葍}努か 提交于 2019-12-10 17:33:42
问题 Is it possible to return the [Required] attribute error message when a JSON request doesn't provide a proper value for an enum property? For example, I have a model for a POST message that contains an AddressType property that is an enumeration type: public class AddressPostViewModel { [JsonProperty("addressType")] [Required(ErrorMessage = "Address type is required.")] public AddressType AddressType { get; set; } } The AddressType enum accepts two values: [JsonConverter(typeof

Asp.Net Core Web app: Global exception handling using IExceptionFilter vs custom middleware

限于喜欢 提交于 2019-12-10 17:16:47
问题 Asp.Net Core supports two ways to do global exception handling for a Web Application, implementing IExceptionFilter or by creating custom middleware. Is there any advantage of one over the other? Most references I see are for creating custom middleware. 回答1: The ASP.NET Core docs explains the main differences between these two approaches. It states that exception filters: Handle unhandled exceptions that occur in Razor Page or controller creation, model binding, action filters, or action

Post Stream in ASP.NET Core Web Api

别等时光非礼了梦想. 提交于 2019-12-10 12:40:02
问题 Hello lovely people of Stack Overflow. Since yesterday I have a problem and I have been browsing SO since then. I have a UWP Client and ASP.NET Core Web Api. I just want to send a stream to my web api but indeed this occurred to be harder task than i thought. I have a class which I have only one property. The Stream property as you can see below: public class UploadData { public Stream InputData { get; set; } } Then Here is my code from my Web Api: // POST api/values [HttpPost] public string

JWT bearer token authorization being applied on exisitng MVC web appication in .NET Core

不想你离开。 提交于 2019-12-10 12:19:56
问题 I am trying out Web API's in .net core. I have added an API controller to my existing .net core MVC web application. I am using JWT Tokens for authorization. I am doing this through the startup class. The API works just fine. But as expected the authorization is being applied on the entire MVC application. Is there a way I can enable authentication through bearer tokens only for the API controllers and not the web application controllers? Startup.cs: using System; using Microsoft.AspNetCore

Duplicate type name within an assembly in Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware

て烟熏妆下的殇ゞ 提交于 2019-12-10 12:12:54
问题 I have .NET Core 1.1 API and i am handling the error in startup.cs as below. I am using Serilog public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime, IRequestContext requestContext) { loggerFactory.AddSerilog(); // Ensure any buffered events are sent at shutdown appLifetime.ApplicationStopped.Register(Log.CloseAndFlush); var logger = loggerFactory.CreateLogger<Startup>(); app.UseExceptionHandler( options => {