Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Razor Pages map HEAD requests to GET handler #6155

Merged
merged 3 commits into from
May 1, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions aspnetcore/mvc/razor-pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess it's a bit verbose to call this out, but precise.

Copy link
Collaborator Author

@guardrex guardrex May 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that if we do do it we'll probably have less trouble with adoption/migration.

We're probably going to get slammed by reader remarks now that they go directly into docs repo issues. I humbly beseech the 🙏 server gods 🙏 for this approach to work and save us a few reader feedback issues of Why isn't this working!?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is opt-in
Opt in to this behavior


```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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • preview 1 --> Preview 1
  • Break this 2nd sentence into 2 sentences.


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`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a word instead of the → symbol. This will be safer for localization purposes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok ... but I wonder if the arrow predates written language! 😄 lol

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you mean the direction it points for right-to-left reading languages. Makes sense.


```csharp
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.AllowMappingHeadRequestsToGetHandler = true;
});
```
::: moniker-end

<a name="xsrf"></a>

## XSRF/CSRF and Razor Pages
Expand Down Expand Up @@ -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.
Expand Down