Skip to content

Instantly share code, notes, and snippets.

View xiaomi7732's full-sized avatar

Saar Shen xiaomi7732

View GitHub Profile
@xiaomi7732
xiaomi7732 / AsyncBeforeHostStart.md
Created March 1, 2024 05:34
Run Async code before hosting get started
@xiaomi7732
xiaomi7732 / aspnetcorefilterparameters.md
Last active October 10, 2023 21:29
How to Create ASP.NET Core Filter with Parameters

Accept the parameter on the attribute

using Microsoft.AspNetCore.Mvc;

namespace FiltersDemo;

public class ExampleFilterAttribute : TypeFilterAttribute
{
    public ExampleFilterAttribute(AnEnum enumValue) : base(typeof(ExampleFilter))
@xiaomi7732
xiaomi7732 / HttpStatusCode.md
Last active September 22, 2023 21:13
HTTP Status Code & Typical Usage

HTTP Status Code & Typical Usage

This page will be used to note down different commonly used status code and the scenarios.

Http Status Code: 200 OK

200 OK, which means it succeeded.

Typically used as a response for synchronous operations.

@xiaomi7732
xiaomi7732 / SpecialRouteValuesInCreateAtAction.md
Created August 10, 2023 01:35
Handling Special Characters in RouteValues for CreatedAtAction

Handling Special Characters in RouteValues for CreatedAtAction

When working with ASP.NET and the CreatedAtAction method, the process of providing route values is often straightforward. Consider the following example:

[HttpPost]
public async Task<IActionResult> Post(ApiVersion apiVersion)
{
    ...
 var routeValues = new 
@xiaomi7732
xiaomi7732 / ReplaceService.md
Created August 8, 2023 00:02
How to replace a service in C# dependency injection

How to replace a service in C# dependency injection

Discover the Power of the Replace Method!

Unveil the hidden gem within C# dependency injection – the Replace method – to effortlessly update services. Experience the simplicity firsthand:

Suppose you're aiming to substitute the initial singleton service of IAnalyzer with the innovative SimpleAnalyzer implementation. Behold the elegance of the solution:

services.Replace(ServiceDescriptor.Singleton());
@xiaomi7732
xiaomi7732 / HashStringToGuid.md
Created August 3, 2023 00:05
Hash a string to a guid

Hash a string to a guid

Sometimes, you may encounter a situation where you require a GUID-formatted key but only have a loose string at hand. In such cases, this simple solution can prove to be quite effective.

Goal

This is the API I want:

Guid CreateGuid(string value);
@xiaomi7732
xiaomi7732 / ApiVersionAttribute.cs
Created July 31, 2023 21:04 — forked from commonsensesoftware/ApiVersionAttribute.cs
Implement Custom API Version Format
public sealed class ApiVersionAttribute : Asp.Versioning.ApiVersionAttribute
{
public ApiVersionAttribute( string version )
: base( CustomApiVersionParser.Default, version ) { }
public ApiVersionAttribute( string token1, string token2, string? token3 = default )
: base( new CustomApiVersion( token1, token2, token3 ) ) { }
}
@xiaomi7732
xiaomi7732 / LinkFile.md
Created July 26, 2023 23:54
Link a file in csproj for compiling

Needs to do this from time to time.

  <ItemGroup>
    <Compile Include="..\ServiceProfiler2\src\ServiceProfiler.Analysis.DataModels\Insights\InsightRecord.cs" Link="InsightRecord.cs" />
  </ItemGroup>
@xiaomi7732
xiaomi7732 / DockerizeASPNetCore.md
Last active June 8, 2023 00:31
Run an ASP.NET Web docker container

Command:

docker run -it -p localport:containerport --rm <containerTag>

Example:

docker run -it -p 8080:80 --rm dotnet-diag-webapi-example:latest
@xiaomi7732
xiaomi7732 / BashIntoContainers.md
Created May 19, 2023 04:58
Bash into containers

Bash into containers

Docker container

docker exec -it <container name> /bin/bash

Kubernetes