asp.net-core-2.0

Asp.Net Core middleware pathstring startswithsegments issue

冷暖自知 提交于 2019-12-07 04:27:49
问题 Have a Asp.NET Core 2.0 application and I would like to map any path that does not start with /api to just reexecute to the root path. I added the below but doesn't seem to work: app.MapWhen( c => !c.Request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase), a => a.UseStatusCodePagesWithReExecute("/") ); Not using MapWhen() and just using app.UseStatusCodePagesWithReExecute("/") works for all paths not root. Just want to add filtering for all paths not root and not /api .

Using Required and JsonRequired in ASP.NET Core Model Binding with JSON body

最后都变了- 提交于 2019-12-07 04:26:20
问题 I'm using ASP.NET Core 2.0, and I have a request object annotated like this: public class MyRequest { [Required] public Guid Id { get; set; } [Required] public DateTime EndDateTimeUtc { get; set; } [Required] public DateTime StartDateTimeUtc { get; set; } } And in my controller: public async Task<IActionResult> HandleRequest([FromBody] MyRequest request) { /* ... */ } I noticed an issue with model binding: When I send a request containing the header Content-Type set to application/json and a

.Net Core IdentityServer4 Get Authenticated User

自作多情 提交于 2019-12-07 04:20:42
问题 I'm trying to figure out how to retrieve a logged in user from Identity server 4 using .Net-Core 2. My authentication is working currently, I'm just trying to figure out how I can retrieve the claims Identity from the HTTP Context. services.AddAuthentication(options => { options.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme; }).AddIdentityServerAuthentication(o =

Cannot resolve scoped service Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.IViewBufferScope from root provider

社会主义新天地 提交于 2019-12-07 03:49:43
问题 I'm trying to use in my ASP.NET Core 2.0 web app this sample RazorViewEngineEmailTemplates to create an html email body from View. But when I run it and my controller gets an ajax request, I get this error: Cannot resolve scoped service Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.IViewBufferScope from root provider It's probably coming from resolving dependencies in the RazorViewToStringRenderer class but I have no idea how to fix this. 回答1: ok, the problem was I used renderer from a

Swagger default value for parameter

落爺英雄遲暮 提交于 2019-12-07 03:18:17
问题 How do I define default value for property in swagger generated from following API? public class SearchQuery { public string OrderBy { get; set; } [DefaultValue(OrderDirection.Descending)] public OrderDirection OrderDirection { get; set; } = OrderDirection.Descending; } public IActionResult SearchPendingCases(SearchQuery queryInput); Swashbuckle generates OrderDirection as required parameter. I would like to be it optional and indicate to client the default value (not sure if swagger supports

Can you set request timeout in asp.net core 2.0 hosted in IIS from C# code?

核能气质少年 提交于 2019-12-07 03:14:41
问题 Is there a way to set requestTimeout from C# instead of needing to set requestTimeout in the web.config ? asp.net core 2.0 hosted in IIS <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> </handlers> <aspNetCore requestTimeout="00:00:04" processPath="dotnet" arguments=".\Foo.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> </system.webServer> <

IdentityServer4 with asp.net core2.0 erron on AuthenticationOptions class

若如初见. 提交于 2019-12-07 03:09:26
I am working on Identity Server 4 with asp.net core 2.0. In ConfigurationServices() method i added: app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions { Authority = "http://localhost:59391", RequireHttpsMetadata = false, ApiName = "api1" }); But it gives compile error that: Reference to type 'AuthenticationOptions' claims it is defined in 'Microsoft.AspNetCore.Authentication', but it could not be found. When i look into assembly code: namespace Microsoft.AspNetCore.Builder { public class IdentityServerAuthenticationOptions : Builder.AuthenticationOptions { //

Global Error Catcher

戏子无情 提交于 2019-12-07 02:46:31
问题 In my project I want to log all the errors happen in the application in a database table. I catch almost all errors using try catch blocks, but I cannot catch the global errors like 4xx and 5xx. There are cases that the application does not redirect to the Exception Handler defined in the Configure method of the Startup.cs like when I type a url which does not exist. app.UseExceptionHandler("/Error"); Is there a way to catch all the unhandled errors occurred in my application? 回答1: You could

Insert character at nth position for each line in a text file

别来无恙 提交于 2019-12-07 02:40:32
I have text files, I need to add a space at the 8th character of each line in the text file. Text files have 1000+ multiple rows How would I conduct this? Original file example: 123456789012345.... abcdefghijklmno.... New file: 12345678 9012345 abcdefgh ijklmno Reading this article is helpful: Add a character on each line of a string Note: Length of text lines can be variable (not sure if it matters, one row can have 20 characters, next line may have 30 characters, etc. All text files are in folder: C:\TestFolder Similar question: Delete character at nth position for each line in a text file

How to enable Application Logs in Azure for Net Core 2 App?

余生颓废 提交于 2019-12-07 02:27:13
问题 I am trying to enable application logs in azure. I have a dummy Net Core 2 App running in an appService in azure. and basically my goal is to see the trace messages in the log stream and in the application log files but I have not found the right way to do this. One of the challenge I have found reading other posts is that they assume a web config in place. 回答1: The documentation for ASP.NET Core 2.2 is here. Firstly, enable Application Logging and choose the appropriate level: This may be