Red
vements
.NET Standard Web 應用程式框架,基於 ASP.NET Core 和 Kestrel 構建,並受到 Express.js 簡單性的啟發
RedHttpServer 可以從 NuGet 安裝: Install-Package RHttpServer
RedHttpServer 的創建是為了易於在其上建置。伺服器同時支援中間件模組和擴充模組
更多擴充和中間件
var server = new RedHttpServer ( 5000 , "public" ) ;
server . Use ( new EcsRenderer ( ) ) ;
server . Use ( new CookieSessions < MySession > ( TimeSpan . FromDays ( 1 ) ) ) ;
// Authentication middleware
async Task < HandlerType > Auth ( Request req , Response res )
{
if ( req . GetData < MySession > ( ) != null )
{
return HandlerType . Continue ;
}
await res . SendStatus ( HttpStatusCode . Unauthorized ) ;
return HandlerType . Final ;
}
var startTime = DateTime . UtcNow ;
// Url parameters
server . Get ( "profile/:username" , Auth , ( req , res ) =>
{
var username = req . Context . ExtractUrlParameter ( "username" ) ;
// ... lookup username in database or similar and fetch profile ...
var user = new { FirstName = "John" , LastName = "Doe" , Username = username } ;
return res . SendJson ( user ) ;
} ) ;
// Using forms
server . Post ( "/login" , async ( req , res ) =>
{
var form = await req . GetFormDataAsync ( ) ;
// ... some validation and authentication ...
await res . OpenSession ( new MySession { Username = form [ "username" ] } ) ;
return await res . SendStatus ( HttpStatusCode . OK ) ;
} ) ;
server . Post ( "/logout" , Auth , async ( req , res ) =>
{
var session = req . GetData < MySession > ( ) ;
await res . CloseSession ( session ) ;
return await res . SendStatus ( HttpStatusCode . OK ) ;
} ) ;
// Simple redirects
server . Get ( "/redirect" , Auth , ( req , res ) => res . Redirect ( "/redirect/test/here" ) ) ;
// File uploads
Directory . CreateDirectory ( "uploads" ) ;
server . Post ( "/upload" , async ( req , res ) =>
{
if ( await req . SaveFiles ( "uploads" ) )
return await res . SendString ( "OK" ) ;
else
return await res . SendString ( "Error" , status : HttpStatusCode . NotAcceptable ) ;
} ) ;
server . Get ( "/file" , ( req , res ) => res . SendFile ( "somedirectory/animage.jpg" ) ) ;
// Using url queries
server . Get ( "/search" , Auth , ( req , res ) =>
{
string searchQuery = req . Queries [ "query" ] ;
string format = req . Queries [ "format" ] ;
// ... perform search using searchQuery and return results ...
var results = new [ ] { "Apple" , "Pear" } ;
if ( format == "xml" )
return res . SendXml ( results ) ;
else
return res . SendJson ( results ) ;
} ) ;
// Markdown rendering
server . Get ( "/markdown" , ( req , res ) => res . RenderFile ( "markdown.md" ) ) ;
// Esc rendering
server . Get ( "/serverstatus" , async ( req , res ) => await res . RenderPage ( "pages/statuspage.ecs" ,
new RenderParams
{
{ "uptime" , ( int ) DateTime . UtcNow . Subtract ( startTime ) . TotalSeconds } ,
{ "version" , Red . RedHttpServer . Version }
} ) ) ;
// Using websockets
server . WebSocket ( "/echo" , async ( req , wsd ) =>
{
wsd . SendText ( "Welcome to the echo test server" ) ;
wsd . OnTextReceived += ( sender , eventArgs ) => { wsd . SendText ( "you sent: " + eventArgs . Text ) ; } ;
return HandlerType . Final ;
} ) ;
// Keep the program running easily (async Program.Main - C# 7.1+)
await server . RunAsync ( ) ;
因為我喜歡 C# 和 .NET Core,但經常需要一個簡單但功能強大的 Web 伺服器來完成某些專案。 Express.js 簡潔且易於使用,因此 API 受到了啟發。
RedHttpServer 是在 MIT 許可下發布的,因此即使在商業項目中也可以免費使用。