Saturday, December 30, 2023

Fixing 'Access-Control-Allow-Origin' error in ASP.NET Core 6/7

 When working with a WEB API if you want to share resources between different domains we need to enable CORS in Program.cs as shown below.


app.UseCors(builder => builder
       .AllowAnyHeader()
       .AllowAnyMethod()
       .AllowAnyOrigin()
    );

Note that it is recommended to have this after app.UseRouting(); middleware.

Cheers,
Samitha

Friday, December 29, 2023

Multiple Directories for Static Files in ASP.NET Core

 I was under the impression Asp.Net core can have only one directory for static files. Recently I came across this article and it shows how we can configure multiple directories for Static Files in ASP.NET Core.

Cheers,

Samitha

Thursday, December 21, 2023

Windows 11/10: uninstall problematic Programs


Recently my machine gave me some trouble when trying to remove some app from it. I came across this useful post having options remove problematic apps/programs from a windows 10/11 PC.

From the given list Method V - Run the Microsoft Install/Uninstall troubleshooter was the quickest one worked for me.


Cheers,

Samitha



Thursday, December 14, 2023

Response Codes in WEB API

 When designing WEB APIs we, as developers are interested in response codes returned after doing an operation.


Following are the response codes and there meanings

  • 1xx: Informational – Indicates transfer protocol-level information.
  • 2xx: Success – This confirms that the client’s request was accepted successfully.
  • 3xx: Redirection – Request is not completed. The client must take some additional action to complete their request.
  • 4xx: Client Error – Indicates that there is some error in API code.
  • 5xx: Server Error – Indicates an error occurred due to some environment settings.

Regards,

Samitha

Sunday, October 29, 2023

GitHub Codespaces

 GitHub Codespaces templates provides you quick and easy development setup you need, Check this video for more information.


Regards,

Samitha

Sunday, October 22, 2023

ASP.NET Core 6/ASP.NET Core 7: Accessing Configuration during startup

 With the changes to Program.cs in .Net Core 7, we can still access confirmation options as below

appsettings.json

  "Settings": {

    "AccessTokenKey": "abcd123",

    "AccessTokenExpirySeconds": 3600,

    "RefreshTokenExpiryDays": 30

  } 

}

Program.cs

var builder = WebApplication.CreateBuilder();

ConfigurationManager config= builder.Configuration; // allows  access to  the config

IWebHostEnvironment environment = builder.Environment;// access the environment

                    var key = builder .Configuration["Settings:AccessTokenKey"]; 

Cheers,

Samitha 

Sunday, October 1, 2023

.NET 7: a connection establish error

 When working with .Net 7 WEB API projects you might have encountered following error when connection to SQL Server.

A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.).

This occurs due to a breaking change in SQL driver.

This issue can be resolved by adding TrustServerCertificate=True attribute to your connection string as shown below.


"ConnectionStrings": {

    "DefaultConnection": "Server=SQL_SERVER_NAME;Database=DATABASE_NAME;Integrated Security=True;TrustServerCertificate=True;",

}

 Cheers,

Samithaa