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 许可下发布的,因此即使在商业项目中也可以免费使用。