-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Endpoint for antiforgerySnippet.js for more flexibility.
- Loading branch information
1 parent
74d56f6
commit c526ae6
Showing
6 changed files
with
78 additions
and
7 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
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,37 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Routing; | ||
|
||
namespace Htmx.TagHelpers; | ||
|
||
public static class HtmxAntiforgeryScriptEndpoints | ||
{ | ||
/// <summary> | ||
/// The path to the antiforgery script that is used from HTML | ||
/// </summary> | ||
public static string Path { get; private set; } = "_htmx/antiforgery.js"; | ||
|
||
/// <summary> | ||
/// Register an endpoint that responds with the HTMX antiforgery script.<br/> | ||
/// IMPORTANT: Remember to add the following script tag to your _Layout.cshtml or Razor view: | ||
/// <![CDATA[ | ||
/// <script src="@HtmxAntiforgeryScriptEndpoints.Path" defer></script> | ||
/// ]]> | ||
/// </summary> | ||
/// <param name="builder">Endpoint builder</param> | ||
/// <param name="path">The path to the antiforgery script</param> | ||
/// <returns>The registered endpoint (Use <seealso cref="Path"/> to reference endpoint)</returns> | ||
public static IEndpointConventionBuilder MapHtmxAntiforgeryScript( | ||
this IEndpointRouteBuilder builder, | ||
string? path = null) | ||
{ | ||
// set Path globally for access | ||
Path = path ?? Path; | ||
|
||
return builder.MapGet(Path, async ctx => | ||
{ | ||
ctx.Response.ContentType = "text/javascript"; | ||
await ctx.Response.WriteAsync(HtmxSnippets.AntiforgeryJavaScript); | ||
}); | ||
} | ||
} |
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
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