Sang.AspNetCore.CommonLibraries 1.0.4

dotnet add package Sang.AspNetCore.CommonLibraries --version 1.0.4                
NuGet\Install-Package Sang.AspNetCore.CommonLibraries -Version 1.0.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sang.AspNetCore.CommonLibraries" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sang.AspNetCore.CommonLibraries --version 1.0.4                
#r "nuget: Sang.AspNetCore.CommonLibraries, 1.0.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sang.AspNetCore.CommonLibraries as a Cake Addin
#addin nuget:?package=Sang.AspNetCore.CommonLibraries&version=1.0.4

// Install Sang.AspNetCore.CommonLibraries as a Cake Tool
#tool nuget:?package=Sang.AspNetCore.CommonLibraries&version=1.0.4                

Sang.AspNetCore.CommonLibraries

NuGet version (Sang.AspNetCore.CommonLibraries)

This is a general library for ASP.NET Core, providing some common functionalities and tools.

English | 简体中文

Features

  • Exception Handling: Provides a filter for unhandled exceptions, allowing customization of exception handling behavior (see FilterExtensions.cs).
  • Model Validation: Provides a filter for model validation, allowing customization of behavior when model validation fails (see FilterExtensions.cs).
  • Message Model: Defines a general message model for API responses (see MessageModel.cs).
  • Message Page: Defines a class for generating HTML message pages, supporting Markdown links (see MessagePage.cs).

Usage

Install-Package Sang.AspNetCore.CommonLibraries

Or

dotnet add package Sang.AspNetCore.CommonLibraries

General Consistent Return Model

Change the return type of your API controller to MessageModel<T>, where T is the type of data you are returning. For example:

[HttpGet]
public MessageModel<string> Get()
{
	return new MessageModel<string>
	{
		Status = 0,
		Msg = "Success",
		Data = "Hello, World!"
	};
}

Add filters for unhandled exceptions and model validation failures:

builder.Services.AddUnhandledExceptionFilter(config =>
{
    config.Status = 500;
    config.StatusCode = 500;
    config.WithTraceId = false; //Whether to return TraceId
    config.Message = "Unhandled Exception";
});

Add a filter for model validation failure:

builder.Services.AddModelValidationExceptionFilter(config =>
{
	config.Status = 400; //Business status code
	config.StatusCode = 400; //HTTP status code
    config.Message = "Model Validation Exception"; //Custom message
});

Generating HTML Message Pages

To generate HTML message pages, you can use the MessagePage class. This class provides some properties for setting the content of the page, and then you can call the Render method to generate the HTML page.

The page UI adopts WeUI version 2.5.11, styled after WeChat official accounts, including title, icon, content, list, links, buttons, and footer links.

public ContentResult Page()
{
    var page = new MessagePage
    {
        Icon = MsgIcon.Safe_Warn,
        HtmlTitle = "Webpage Title",
        Title = "Message Title",
        Desc = "Detail content, can be arranged according to actual needs. If it wraps, it should not exceed the specified length and should be centered [hyperlink support](http://www.baidu.com)",
        DescInfo = "Detail content, can be arranged according to actual needs. If it wraps, it should not exceed the specified length and should be centered",
        Custom = "<textarea>Custom html area</textarea>",
        ListInfo = new List<string> { "List hint item", "List hint item", "List hint item" },
        KeyValues = new Dictionary<string, string> { { "Name", "Zhang San" }, { "WeChat ID", "123" } },
        ListUrl = new List<UrlInfo> { new("Link 1", "javascript:"), new("Link 2", "javascript:") },
        TipsPre = "Detail content, can be arranged according to actual needs. If it wraps, it should not exceed the specified length and should be centered [hyperlink support](http://www.baidu.com)",
        TipsNext = "Detail content, can be arranged according to actual needs. If it wraps, it should not exceed the specified length and should be centered [hyperlink support](http://www.baidu.com)",
        OprBtn = new List<UrlInfo> { new("Link 1", "javascript:"), new ("Link 2", "javascript:", "default") },
        FooterLink = new UrlInfo("Footer Link", "javascript:"),
        CopyRight = "Sang Late Autumn Copyright Information<br>© 2014-2021 Sang. All Rights Reserved."
    };
    return new ContentResult
    {
        ContentType = "text/html",
        StatusCode = 200,
        Content = page.Render()
    };
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 90 7/29/2024
1.0.3 99 6/8/2024
1.0.2 103 6/6/2024
1.0.1 101 6/6/2024
1.0.0 94 6/6/2024