Quickest Way to Enable CORS for ASP.NET Core Web API application

Browser security does not allow other domain to make AJAX requests.This is called same-origin policy which prevents malicious attack from other sites. When you need to allow other sites to make cross-origin requests, we can enable Cross-origin resource sharing (CORS).

There is a pretty good documentation about enabling CORS for a dotnet core application here.

In terms of Web API application, the quickest way is to add UserCors in Configure method in startup.cs file as below.

Obviously, the above solution should not be used in production. It is supposed to be a quick test in development environment to make your AJAX requests from another domain work.

In reality, we should have specific origins configured alongside with allowed method or headers. Below is the example of allowing cross-origin requests from the specific domain.

Check out a full ASP.NET API example here.

Front-End
TypeScript: type aliases to check type equality

This post is to analyse how the type equality check by using type aliases proposed by Matt Pocock in his twitter post. These type aliases allow us to elegantly express type equality checks in unit tests. All we need to do is to pass the output and expected types in …

Front-End
Fixing it.only type error in Jest

If you are getting a type error with it.only in Jest, it could be due to incorrect TypeScript typings or incompatible versions of Jest and TypeScript. To resolve this issue, you can try the following steps: Make sure you have the latest versions of Jest and its TypeScript typings installed …

Front-End
yup conditional validation example

Here’s an example of a Yup validation logic where the first input field is optional but, if filled, it must contain only alphabetic characters, and the second input field is required: import * as Yup from “yup”; const validationSchema = Yup.object().shape({ firstField: Yup.string().matches(/^[A-Za-z]*$/, { message: “First field must contain only …