Permalink
Browse files

Execute matching fallback route if targetted or prioritized

  • Loading branch information...
mythz committed Jan 29, 2019
1 parent 62403cf commit a19d4768a5aabe8323e628d4574d016fa81976c1
@@ -210,7 +210,10 @@ public void RegisterRestPaths(Type requestType)
var attrs = appHost.GetRouteAttributes(requestType);
foreach (RouteAttribute attr in attrs)
{
var restPath = new RestPath(requestType, attr.Path, attr.Verbs, attr.Summary, attr.Notes, attr.Matches);
var restPath = new RestPath(requestType, attr.Path, attr.Verbs, attr.Summary, attr.Notes, attr.Matches)
{
Priority = attr.Priority
};

if (attr is FallbackRouteAttribute defaultAttr)
{
@@ -144,16 +144,20 @@ public IHttpHandler GetHandler(HttpContext ctx, string requestType, string url,
//Default Request /
if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
{
//e.g. to Process View Engine requests
var catchAllHandler = GetCatchAllHandlerIfAny(appHost, httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
if (catchAllHandler != null) return catchAllHandler;
RestPath matchesFallback = appHost.Config.FallbackRestPath?.Invoke(httpReq);
if (matchesFallback == null || matchesFallback.Priority > 0 ||
(matchesFallback.MatchRule == null && !(matchesFallback.Priority < 0))) // is not targeted fallback
{
//e.g. to Process View Engine requests
var catchAllHandler = GetCatchAllHandlerIfAny(appHost, httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
if (catchAllHandler != null) return catchAllHandler;
}

//If the fallback route can handle it, let it
var restPath = appHost.Config.FallbackRestPath?.Invoke(httpReq);
if (restPath != null)
if (matchesFallback != null)
{
var sanitizedPath = RestHandler.GetSanitizedPathInfo(pathInfo, out var contentType);
return new RestHandler { RestPath = restPath, RequestName = restPath.RequestType.GetOperationName(), ResponseContentType = contentType };
return new RestHandler { RestPath = matchesFallback, RequestName = matchesFallback.RequestType.GetOperationName(), ResponseContentType = contentType };
}

if (mode == null)
@@ -188,16 +192,20 @@ public static IHttpHandler GetHandler(IHttpRequest httpReq)
//Default Request /
if (string.IsNullOrEmpty(pathInfo) || pathInfo == "/")
{
//e.g. to Process View Engine requests
var catchAllHandler = GetCatchAllHandlerIfAny(appHost, httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
if (catchAllHandler != null) return catchAllHandler;
RestPath matchesFallback = appHost.Config.FallbackRestPath?.Invoke(httpReq);
if (matchesFallback == null || matchesFallback.Priority > 0 ||
(matchesFallback.MatchRule == null && !(matchesFallback.Priority < 0))) // is not targeted fallback
{
//e.g. to Process View Engine requests
var catchAllHandler = GetCatchAllHandlerIfAny(appHost, httpReq.HttpMethod, pathInfo, httpReq.GetPhysicalPath());
if (catchAllHandler != null) return catchAllHandler;
}

//If the fallback route can handle it, let it
RestPath restPath = appHost.Config.FallbackRestPath?.Invoke(httpReq);
if (restPath != null)
if (matchesFallback != null)
{
var sanitizedPath = RestHandler.GetSanitizedPathInfo(pathInfo, out var contentType);
return new RestHandler { RestPath = restPath, RequestName = restPath.RequestType.GetOperationName(), ResponseContentType = contentType };
return new RestHandler { RestPath = matchesFallback, RequestName = matchesFallback.RequestType.GetOperationName(), ResponseContentType = contentType };
}

if (mode == null)
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
@@ -13,13 +13,15 @@
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ServiceStack.Text" Version="$(Version)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ServiceStack.Client\ServiceStack.Client.csproj" />
<ProjectReference Include="..\..\src\ServiceStack.Common\ServiceStack.Common.csproj" />
<ProjectReference Include="..\..\src\ServiceStack.Interfaces\ServiceStack.Interfaces.csproj" />
<ProjectReference Include="..\..\src\ServiceStack.Server\ServiceStack.Server.csproj" />
<ProjectReference Include="..\..\src\ServiceStack\ServiceStack.csproj" />
<PackageReference Include="ServiceStack.Text" Version="$(Version)" />
</ItemGroup>

</Project>

0 comments on commit a19d476

Please sign in to comment.