asp.net-core-webapi

Short Circuit Model Validation C#

淺唱寂寞╮ 提交于 2019-12-11 17:58:54
问题 I'm using .NET Core 2.0. I have a POST action on my controller. It binds the data from the body of the request (JSON) into a C# DTO. On the DTO I have validation attributes - for instance: [Required] [MaxLength(512)] public string email { get; set; } These validators execute in the order that they are defined on the property. I would like to short-circuit the validation if a prior validator fails. For example - if the required validation fails, don't execute the max length validation. Is

ASP Core: how to configure area for api controller without AreaAttribute (or how to enable convention area routing for Api controller)?

99封情书 提交于 2019-12-11 17:56:31
问题 Razor Pages do not need any AreaAttribute and it is enough to place them into Areas/MyArea/Pages folder and this is enough to enable routing /MyArea/MyPage + My Proj + Areas + MyArea + Pages - MyPage.cshtml But when if I put API Controlller MyApi.cs to MyArea folder: + My Proj + Areas + MyArea + Pages - MyApi.cs then I am become forced to "hardcode" area name [Area("MyArea")] // !!! can't be missed [Route("[area]/api/[action]")] [Produces("application/json")] [ApiController] public class

IHttpClientFactory intermittent error: An error occurred while sending the request with Azure Function Apps

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 17:53:45
问题 The error below occurs intermittently when _httpClientFactory.CreateClient() is used to send request to external resources. System.Net.Http.HttpRequestException : An error occurred while sending the request. ---> System.IO.IOException : The server returned an invalid or unrecognized response. at async System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request,CancellationToken cancellationToken) End of inner exception at async System.Net.Http.HttpConnection.SendAsyncCore

ASP.NET CORE Web API: Model value is null when doing HTTP Post requests with null Guid

无人久伴 提交于 2019-12-11 16:55:40
问题 I'm facing a problem doing post requests via ASP.NET Core Web API. I'm a bit confused of the articles I read/found while Googling. So here's my scenario: I'm trying to do a HTTP Post request to an api endpoint with a complex parameter. [HttpPost] [Route("api/createstudent")] public async Task<ActionResult> CreateStudent([FromBody]Student student) { // Service logic } Model: public class Student { public Guid StudentId { get; set; } public string StudentName { get; set; } } My problem is that

Can I keep an unserialisable object between requests?

谁都会走 提交于 2019-12-11 15:26:46
问题 I have created an .NET Core API project. It works but it creates a new object for each get request. Where can I save the object so that I can reuse it? I have searched Google and found Session but that only seems to store bytes or serialised bytes. I don't think I can serialise the object. If doSomeInitialisation() takes 1 seconds, creating a new SomeLibrary at each get request when the arg1 is the same does not seem efficient and SomeLibrary is not serialisable so it cannot be stored to the

Authenticate user without login for web api using microsoft graph

三世轮回 提交于 2019-12-11 14:23:10
问题 I want to use microsoft graph in web api which is using .net core 2.0, I want to authenticate user without login prompt. Following is the code I have used, Please correct me if my method is wrong. string clientId = ""; string clientsecret = ""; string tenant = ""; string resourceURL = "https://graph.microsoft.com"; string userMail = "testuser3@company.com"; public async Task<string> GetMyEmailUser() { try { var result = ""; string AzureADAuthority = "https://login.microsoftonline.com

How to get new access token by using refresh token in AspNet.Security.OpenIdConnect.Server (ASOS)

独自空忆成欢 提交于 2019-12-11 14:08:30
问题 I am using React as client and Web API core for back end interaction. For Authentication we are using Token based authentication using AspNet.Security.OpenIdConnect.Server (ASOS) . I have to implement refresh token scenario where on expiration of access token we use refresh token (returned by ASOS) to get new access Token. I know one way to achieve by calling method on client is in AXIOS interceptor like below. httpPromise.interceptors.response.use(undefined, err => { const { config, response

Kendo Angular Upload Adds to Start of Text File?

◇◆丶佛笑我妖孽 提交于 2019-12-11 14:06:13
问题 I'm using the Kendo Upload control in an Angular app and it's 99% working, but it seems to be adding a small piece of text to the start of the text file I'm uploading. Here is the HTML definition: <kendo-upload [saveUrl]="SaveUrl" (upload)="UploadHandle($event)" (error)="UploadError($event)" [restrictions]="Restrictions" (success)="UploadSuccess($event)"></kendo-upload> The upload handler: UploadHandle(e: UploadEvent): void { e.data = { type: 'MasterFile' }; } The restrictions and save URL

How to pass parameter from Ajax Request to Web API Controller?

会有一股神秘感。 提交于 2019-12-11 12:14:36
问题 I am looking for the way to pass the parameter from Ajax Request to Web API Controller in ASP.Net Core like query string in classic ASP. I have tried below but it did not worked. View: "ajax": { "url": "/api/APIDirectory/GetDirectoryInfo?reqPath=@ViewBag.Title" "type": "POST", "dataType": "JSON" }, Controller: [HttpPost] public IActionResult GetDirectoryInfo(string reqPath) { string requestPath = reqPath; // some code here.. } Can anyone please advise the ways available to achieve this in asp

Is it possible to hide an Enum member in Swashbuckle.AspNetCore?

扶醉桌前 提交于 2019-12-11 11:28:10
问题 I have an enum such as public enum SampleFormats { unknown = 0, audio = 1, video = 2, } Is it possible to decorate the unknown member in a way that it is excluded by the generated swagger json? I could possibly write a schema/document filter, but was wondering if there was something out of the box. 回答1: You can try this: public enum SampleFormats { unknown = 0, audio = 1, video = 2, } public class ResultModel { public SampleFormats Format { get; set; } [JsonIgnore] public bool FormatSpecified