HomeRoute removed (use MapRoute "/")
This commit is contained in:
parent
1eb208cba3
commit
a4c2ae3e28
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib.Web</PackageId>
|
<PackageId>DTLib.Web</PackageId>
|
||||||
<Version>1.2.0</Version>
|
<Version>1.2.1</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>HTTP Server with simple routing</Description>
|
<Description>HTTP Server with simple routing</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
|
|||||||
@ -2,12 +2,9 @@ namespace DTLib.Web.Routes;
|
|||||||
|
|
||||||
public class SimpleRouter : IRouter
|
public class SimpleRouter : IRouter
|
||||||
{
|
{
|
||||||
/// route for base url
|
|
||||||
public RouteHandler? HomePageRoute { get; set; }
|
|
||||||
/// route for any url that doesn't have its own handler
|
/// route for any url that doesn't have its own handler
|
||||||
public RouteHandler? DefaultRoute { get; set; }
|
public RouteHandler? DefaultRoute { get; set; }
|
||||||
|
|
||||||
|
|
||||||
private readonly Dictionary<string, RouteHandler> _routes = new();
|
private readonly Dictionary<string, RouteHandler> _routes = new();
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
@ -23,11 +20,11 @@ public class SimpleRouter : IRouter
|
|||||||
|
|
||||||
public async Task<HttpStatusCode> Resolve(HttpListenerContext ctx)
|
public async Task<HttpStatusCode> Resolve(HttpListenerContext ctx)
|
||||||
{
|
{
|
||||||
string requestPath = ctx.Request.Url?.AbsolutePath ?? "/";
|
|
||||||
RouteHandler? route;
|
string? requestPath = ctx.Request.Url?.AbsolutePath;
|
||||||
if(HomePageRoute != null && requestPath == "/")
|
if (string.IsNullOrEmpty(requestPath))
|
||||||
route = HomePageRoute;
|
requestPath = "/";
|
||||||
else if (!_routes.TryGetValue($"{requestPath}:{ctx.Request.HttpMethod}", out route))
|
if (!_routes.TryGetValue($"{requestPath}:{ctx.Request.HttpMethod}", out var route))
|
||||||
route = DefaultRoute;
|
route = DefaultRoute;
|
||||||
|
|
||||||
HttpStatusCode status;
|
HttpStatusCode status;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user