Skip to content

Commit

Permalink
Add for-route attribute to <button> as an alternative to asp-page-han…
Browse files Browse the repository at this point in the history
…dler
  • Loading branch information
daviddotcs committed Dec 24, 2023
1 parent 6ba131f commit e42b4fe
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Demo/SafeRouting.Demo/Pages/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@{
var controllerRoute = Routes.Controllers.Product.Search("chair", 10);
var pageRoute = Routes.Pages.Edit.Post(Model.Id);
var pageHandlerRoute = Routes.Pages.Product.PostDelete();
}

<!-- Adds the URL in the href attribute -->
Expand All @@ -16,4 +17,9 @@
<!-- Adds the URL in the action attribute -->
<form for-route="pageRoute" method="post"></form>

<!-- Adds the URL in the button's formaction attribute -->
<form method="post">
<button type="submit" for-route="pageHandlerRoute">Delete</button>
</form>

@{ #endregion }
2 changes: 2 additions & 0 deletions src/Demo/SafeRouting.Demo/Pages/Product.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public void OnGet() { }
public IActionResult OnGetRedirectToController() => Routes.Controllers.Account.Index().Redirect(this);

public IActionResult OnGetRedirectToPage() => Routes.Pages.Index.Get().Redirect(this);

public void OnPostDelete() { }
}
2 changes: 1 addition & 1 deletion src/README.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ To enable the included tag helpers, add the following line to `_ViewImports.csht

<region:ViewImports>

This enables `for-route` attributes to be added to `<a>`, `<img>`, and `<form>` elements, for example:
This enables `for-route` attributes to be added to `<a>`, `<button>`, `<img>`, and `<form>` elements, for example:

<region:CshtmlExample>

Expand Down
4 changes: 4 additions & 0 deletions src/SafeRouting.Common/TagHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace SafeRouting.TagHelpers;
/// <see cref="ITagHelper"/> implementation targeting <c>&lt;a&gt;</c>, <c>&lt;form&gt;</c>, or <c>&lt;img&gt;</c> elements.
/// </summary>
[HtmlTargetElement(AnchorTagName, Attributes = ForRouteAttributeName)]
[HtmlTargetElement(ButtonTagName, Attributes = ForRouteAttributeName)]
[HtmlTargetElement(FormTagName, Attributes = ForRouteAttributeName)]
[HtmlTargetElement(ImageTagName, Attributes = ForRouteAttributeName)]
public sealed class RouteValuesTagHelper : TagHelper
Expand Down Expand Up @@ -43,6 +44,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
var urlAttributeName = context.TagName switch
{
AnchorTagName => AnchorAttributeName,
ButtonTagName => ButtonAttributeName,
FormTagName => FormAttributeName,
ImageTagName => ImageAttributeName,
_ => null
Expand All @@ -65,6 +67,8 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
private const string ForRouteAttributeName = "for-route";
private const string AnchorTagName = "a";
private const string AnchorAttributeName = "href";
private const string ButtonTagName = "button";
private const string ButtonAttributeName = "formaction";
private const string FormTagName = "form";
private const string FormAttributeName = "action";
private const string ImageTagName = "img";
Expand Down

0 comments on commit e42b4fe

Please sign in to comment.