-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccdb762
commit 076e2d8
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# SPA routing | ||
|
||
I like separating the `SPA` and `API` codebase (they could still live in the same repository but in different directories). If you do end up deploying the `SPA` in the `wwwroot/` directory of the `API` | ||
|
||
```csharp | ||
public class SpaRoutingMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public SpaRoutingMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public Task InvokeAsync(HttpContext context) | ||
{ | ||
if (IsGetMethod(context) && !IsApiPath(context) && !HasExtension(context)) | ||
{ | ||
context.Request.Path = "/index.html"; | ||
} | ||
|
||
return _next(context); | ||
} | ||
|
||
private static bool IsGetMethod(HttpContext context) | ||
{ | ||
return context.Request.Method == HttpMethods.Get; | ||
} | ||
|
||
private static bool IsApiPath(HttpContext context) | ||
{ | ||
return context.Request.Path.StartsWithSegments("/api"); | ||
} | ||
|
||
private static bool HasExtension(HttpContext context) | ||
{ | ||
if (!context.Request.Path.HasValue) | ||
return false; | ||
|
||
var startIndex = context.Request.Path.Value.LastIndexOf('.'); | ||
|
||
return startIndex >= 0; | ||
} | ||
} | ||
``` |