Skip to content

Commit

Permalink
Add example for MapFallbackToFile
Browse files Browse the repository at this point in the history
  • Loading branch information
dodyg committed Apr 27, 2019
1 parent 7a4e2f7 commit 702bb19
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,4 +1,4 @@
# 237 samples for ASP.NET Core 2.1, 2.2 and 3.0-preview-4 fundamentals (updated almost daily - except during Summer)
# 238 samples for ASP.NET Core 2.1, 2.2 and 3.0-preview-4 fundamentals (updated almost daily - except during Summer)

If you are studying ASP.NET Core, I am lurking on this **[Gitter Channel](https://gitter.im/DotNetStudyGroup/aspnetcore)**.

Expand All @@ -17,7 +17,7 @@ ASP.NET Core API Browser is also very [handy](https://docs.microsoft.com/en-us/d

| Section | No. of Samples | .NET Core SDK Version |
| ------- | ------- | ------- |
| [ASP.NET Core 3.0](/projects/3-0) | 10 | 3.0-preview-4 |
| [ASP.NET Core 3.0](/projects/3-0) | 11 | 3.0-preview-4 |
| [Blazor Server Side](/projects/blazor-ss) | 5 | 3.0-preview-4 |
| [Blazor Client Side (Web Assembly)](/projects/blazor/README.md) | 12 | 3.0-preview-4 |
| [ASP.NET Core MVC](/projects/mvc/README.md) | 43 | 2.1 |
Expand Down
6 changes: 5 additions & 1 deletion projects/3-0/README.md
@@ -1,4 +1,4 @@
# .NET Core 3.0 Preview 4 (10)
# .NET Core 3.0 Preview 4 (11)

All the samples here rely on ASP.NET Core 3.0 Preview 4. Make sure you download the SDK [here](https://dotnet.microsoft.com/download/dotnet-core/3.0).

Expand Down Expand Up @@ -90,3 +90,7 @@ The official migration guide from 2.2 to 3.0 is [here](https://docs.microsoft.co
* [New Routing - RequestDelegate with HTTP verb filter](/projects/3-0/new-routing-7)

This example shows how to use `RequestDelegate` directly in `app.UseEndpoints` using `MapMethods` that filter request based on one or more HTTP verbs.

* [New Routing - Default static page handler](/projects/3-0/new-routing-8)

Return a static page when your request does not match anything else using `MapFallbackToFile`.
3 changes: 0 additions & 3 deletions projects/3-0/new-routing-8/Pages/About.cshtml

This file was deleted.

4 changes: 2 additions & 2 deletions projects/3-0/new-routing-8/README.md
@@ -1,3 +1,3 @@
# New Routing - using RequestDelegate with HTTP verb filter
# New Routing - Static file fallback

Respond to a url pattern and filter the request based on HTTP verbs.
Return a static page when your request does not match anything else using `MapFallbackToFile`.
17 changes: 15 additions & 2 deletions projects/3-0/new-routing-8/src/Program.cs
Expand Up @@ -9,6 +9,8 @@
using Microsoft.AspNetCore.Connections;
using System.Buffers;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;
using System.IO;

namespace NewRouting
{
Expand All @@ -19,10 +21,21 @@ public void ConfigureServices(IServiceCollection services)
services.AddMvc();
}

public void Configure(IApplicationBuilder app)
public void Configure(IApplicationBuilder app, IHostEnvironment environment)
{
app.UseRouting(route =>
app.UseRouting();

app.UseEndpoints(routes =>
{
routes.MapGet("/contact/us", async context =>
{
await context.Response.WriteAsync("Contact Us");
});
routes.MapFallbackToFile("index.html", new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(environment.ContentRootPath, "Static")),
});
});
}
}
Expand Down
12 changes: 12 additions & 0 deletions projects/3-0/new-routing-8/static/index.html
@@ -0,0 +1,12 @@
<html>

<body>
<h1>This is a static page that got served when nothing else matches your request</h1>
<ul>
<li><a href="/contact/us">/contact/us</a></li>
<li><a href="/about">/about</a></li>
<li><a href="/buy-this">/buy-this</a></li>
</ul>
</body>

</html>

0 comments on commit 702bb19

Please sign in to comment.