From 808a711a7b63d3f13e1e5a9fd34cbbd28af1cb29 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Tue, 1 May 2018 13:38:57 -0500 Subject: [PATCH 1/3] Razor Pages map HEAD requests to GET handler --- aspnetcore/mvc/razor-pages/index.md | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/aspnetcore/mvc/razor-pages/index.md b/aspnetcore/mvc/razor-pages/index.md index 92c2b48b3a68..426e39ab1eee 100644 --- a/aspnetcore/mvc/razor-pages/index.md +++ b/aspnetcore/mvc/razor-pages/index.md @@ -204,6 +204,38 @@ The `OnPostDeleteAsync` method: * If the customer contact is found, they're removed from the list of customer contacts. The database is updated. * Calls `RedirectToPage` to redirect to the root Index page (`/Index`). +::: moniker range=">= aspnetcore-2.1" +## Manage HEAD requests with the OnGet handler + +Ordinarily, a HEAD handler is created and called for HEAD requests: + +```csharp +public void OnHead() +{ + HttpContext.Response.Headers.Add("HandledBy", "Handled by OnHead!"); +} +``` + +If no HEAD handler (`OnHead`) is defined, Razor Pages falls back to calling the GET page handler (`OnGet`) in ASP.NET Core 2.1 or later. This behavior is opt-in with the [SetCompatibilityVersion method](xref:fundamentals/startup#setcompatibilityversion-for-aspnet-core-mvc) in `Startup.Configure` for ASP.NET Core 2.1→2.x: + +```csharp +services.AddMvc() + .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1); +``` + +`SetCompatibilityVersion` effectively sets the Razor Pages option `AllowMappingHeadRequestsToGetHandler` to `true`. The behavior is opt-in until the release of ASP.NET Core 3.0 preview 1 or later because each major version of ASP.NET Core adopts all of the patch release behaviors of the previous version. + +Global opt-in behavior for patch releases 2.1→2.x can be avoided with an app configuration that maps HEAD requests to the GET handler. Set the `AllowMappingHeadRequestsToGetHandler` Razor Pages option to `true` without calling `SetCompatibilityVersion` in `Startup.Configure`: + +```csharp +services.AddMvc() + .AddRazorPagesOptions(options => + { + options.AllowMappingHeadRequestsToGetHandler = true; + }); +``` +::: moniker-end + ## XSRF/CSRF and Razor Pages @@ -362,6 +394,8 @@ The preceding code uses *named handler methods*. Named handler methods are creat Using the preceding code, the URL path that submits to `OnPostJoinListAsync` is `http://localhost:5000/Customers/CreateFATH?handler=JoinList`. The URL path that submits to `OnPostJoinListUCAsync` is `http://localhost:5000/Customers/CreateFATH?handler=JoinListUC`. + + ## Customizing Routing If you don't like the query string `?handler=JoinList` in the URL, you can change the route to put the handler name in the path portion of the URL. You can customize the route by adding a route template enclosed in double quotes after the `@page` directive. From 4ce66a12281cc122f75b71e1e7a1fcbb6e64ff17 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Tue, 1 May 2018 14:50:18 -0500 Subject: [PATCH 2/3] React to feedback --- aspnetcore/mvc/razor-pages/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aspnetcore/mvc/razor-pages/index.md b/aspnetcore/mvc/razor-pages/index.md index 426e39ab1eee..f684b038706c 100644 --- a/aspnetcore/mvc/razor-pages/index.md +++ b/aspnetcore/mvc/razor-pages/index.md @@ -216,7 +216,7 @@ public void OnHead() } ``` -If no HEAD handler (`OnHead`) is defined, Razor Pages falls back to calling the GET page handler (`OnGet`) in ASP.NET Core 2.1 or later. This behavior is opt-in with the [SetCompatibilityVersion method](xref:fundamentals/startup#setcompatibilityversion-for-aspnet-core-mvc) in `Startup.Configure` for ASP.NET Core 2.1→2.x: +If no HEAD handler (`OnHead`) is defined, Razor Pages falls back to calling the GET page handler (`OnGet`) in ASP.NET Core 2.1 or later. Opt in to this behavior with the [SetCompatibilityVersion method](xref:fundamentals/startup#setcompatibilityversion-for-aspnet-core-mvc) in `Startup.Configure` for ASP.NET Core 2.1 to 2.x: ```csharp services.AddMvc() @@ -225,7 +225,7 @@ services.AddMvc() `SetCompatibilityVersion` effectively sets the Razor Pages option `AllowMappingHeadRequestsToGetHandler` to `true`. The behavior is opt-in until the release of ASP.NET Core 3.0 preview 1 or later because each major version of ASP.NET Core adopts all of the patch release behaviors of the previous version. -Global opt-in behavior for patch releases 2.1→2.x can be avoided with an app configuration that maps HEAD requests to the GET handler. Set the `AllowMappingHeadRequestsToGetHandler` Razor Pages option to `true` without calling `SetCompatibilityVersion` in `Startup.Configure`: +Global opt-in behavior for patch releases 2.1 to 2.x can be avoided with an app configuration that maps HEAD requests to the GET handler. Set the `AllowMappingHeadRequestsToGetHandler` Razor Pages option to `true` without calling `SetCompatibilityVersion` in `Startup.Configure`: ```csharp services.AddMvc() From f9256a3612d9b1b8463addb0b984b65e54cc69ea Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Tue, 1 May 2018 15:26:22 -0500 Subject: [PATCH 3/3] React to feedback --- aspnetcore/mvc/razor-pages/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/mvc/razor-pages/index.md b/aspnetcore/mvc/razor-pages/index.md index f684b038706c..1910bbcc5bb6 100644 --- a/aspnetcore/mvc/razor-pages/index.md +++ b/aspnetcore/mvc/razor-pages/index.md @@ -223,7 +223,7 @@ services.AddMvc() .SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1); ``` -`SetCompatibilityVersion` effectively sets the Razor Pages option `AllowMappingHeadRequestsToGetHandler` to `true`. The behavior is opt-in until the release of ASP.NET Core 3.0 preview 1 or later because each major version of ASP.NET Core adopts all of the patch release behaviors of the previous version. +`SetCompatibilityVersion` effectively sets the Razor Pages option `AllowMappingHeadRequestsToGetHandler` to `true`. The behavior is opt-in until the release of ASP.NET Core 3.0 Preview 1 or later. Each major version of ASP.NET Core adopts all of the patch release behaviors of the previous version. Global opt-in behavior for patch releases 2.1 to 2.x can be avoided with an app configuration that maps HEAD requests to the GET handler. Set the `AllowMappingHeadRequestsToGetHandler` Razor Pages option to `true` without calling `SetCompatibilityVersion` in `Startup.Configure`: