Posts

Showing posts from January, 2021

How to download file from the blob storage in CSharp

To download a file from a blob storage, first you have to download the reference or assembly from the nuget package "Azure.Storage.Blobs". You also have to obtain the connection string from azure blob storage account private BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); var container = _blobServiceClient.GetBlobContainerClient(containerName); await container.CreateIfNotExistsAsync(Azure.Storage.Blobs.Models.PublicAccessType.BlobContainer); var blob = container.GetBlobClient("image.png"); await blob.DownloadToAsync("c:\users\documents\image.png");

Asp.net core swagger not showing parameter description

In order to display the description of the parameter specified in the action. You have to enable xml generation in your project property and enable it in swagger option. services.AddSwaggerGen(swagger => { swagger.SwaggerDoc("v1", new OpenApiInfo { Title = "ArchiveOne WebApi", Version = "v1" }); var basePath = PlatformServices.Default.Application.ApplicationBasePath; foreach (var name in Directory.GetFiles(basePath, "*.XML", SearchOption.AllDirectories)) { swagger.IncludeXmlComments(name); } });

This CodeDomProvider does not support this method

This error occurs when you try to parse CSharp code using CodeDomProvider . CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); var compileUnit = provider.Parse(File.OpenText(filename)); In order to parse existing CSharp code you have to use Roslyn or other parser.

How to add custom header in swagger operation

Image
I am currently developing a .net core web API that will use a swagger as its documentation.  Everything was perfect, except almost of the request method has a required data that was in the header.  My problem was how to add an input header in the Swagger UI. After a few research, I found it. Here's how to do it. Create a filter class that implement IOperationFilter public class MyCustomHeaderFilter : IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { if (operation.Parameters != null) { operation.Parameters.Add(new OpenApiParameter { Name = "FilterName", In = ParameterLocation.Header, Description = "access token", Required = false, }); } } } Register the class in swagger using OperationFilter ...

Best way to download a file using HttpClient

public async Task DownloadAsync() { using (HttpClient client = new HttpClient()) { string url = "https://github.com/App-vNext/Polly/archive/master.zip"; using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)) using (Stream fromStream = await response.Content.ReadAsStreamAsync()) { string targetFilename = Path.GetTempFileName(); using (Stream targetStream = File.Open(targetFilename, FileMode.Create)) { await fromStream.CopyToAsync(targetStream); } } } }

The specified version of Microsoft.NetCore.App or Microsoft.AspNetCore.App was not found.

This means that .net core runtime is missing, just download it from here

How to decode url in asp.net mvc

HttpUtility.UrlDecode(encodedStr); In .Net Core WebUtility.UrlDecode(encodedStr)