Skip to content

Commit

Permalink
Add LinkGenerator extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddotcs committed Aug 27, 2023
1 parent 282b238 commit d245d87
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 5 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Similarly, consider the following razor page model class:
```csharp
public sealed class EditModel : PageModel
{
[FromRoute]
public int Id { get; set; }

public void OnGet()
{
// ...
Expand All @@ -76,7 +79,7 @@ public sealed class EditModel : PageModel
The generated code enables you to access the URL for the `OnGet` handler with the following code:

```csharp
string? editUrl = Routes.Pages.Edit.Get().Url(Url);
string? editUrl = Routes.Pages.Edit.Get(123).Url(Url);
```

## Installation
Expand All @@ -98,7 +101,7 @@ This enables `for-route` attributes to be added to `<a>`, `<img>`, and `<form>`
```cshtml
@{
var controllerRoute = Routes.Controllers.Product.Search("chair", 10);
var pageRoute = Routes.Pages.Edit.Post();
var pageRoute = Routes.Pages.Edit.Post(Model.Id);
}
<!-- Adds the URL in the href attribute -->
Expand Down Expand Up @@ -149,6 +152,15 @@ route.Remove(route.Properties.Limit);
// Value: "/Product/Search/book"
string? routeUrl = route.Url(Url);

// Get route information for the OnGet method on the /Edit page
var pageRoute = Routes.Pages.Edit.Get(123);

// "/Edit?Id=123"
var path = pageRoute.Path(linkGenerator);

// "https://example.org/Edit?Id=123"
var uri = pageRoute.Url(linkGenerator, "https", new HostString("example.org"));

// Redirect from within a controller action method or a page handler method
return route.Redirect(this);
```
Expand Down
13 changes: 11 additions & 2 deletions src/Demo/SafeRouting.Demo/Controllers/UsageExampleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public IActionResult SourceGeneratorRedirect()
public string? PageUrl()
{
#region EditUrl
string? editUrl = Routes.Pages.Edit.Get().Url(Url);
string? editUrl = Routes.Pages.Edit.Get(123).Url(Url);
#endregion
return editUrl;
}

public IActionResult GettingStarted()
public IActionResult GettingStarted([FromServices] LinkGenerator linkGenerator)
{
#pragma warning disable IDE0059 // Unnecessary assignment of a value
#region GettingStarted
Expand Down Expand Up @@ -57,6 +57,15 @@ public IActionResult GettingStarted()
// Value: "/Product/Search/book"
string? routeUrl = route.Url(Url);

// Get route information for the OnGet method on the /Edit page
var pageRoute = Routes.Pages.Edit.Get(123);

// "/Edit?Id=123"
var path = pageRoute.Path(linkGenerator);

// "https://example.org/Edit?Id=123"
var uri = pageRoute.Url(linkGenerator, "https", new HostString("example.org"));

// Redirect from within a controller action method or a page handler method
return route.Redirect(this);
#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Demo/SafeRouting.Demo/Pages/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@{
var controllerRoute = Routes.Controllers.Product.Search("chair", 10);
var pageRoute = Routes.Pages.Edit.Post();
var pageRoute = Routes.Pages.Edit.Post(Model.Id);
}

<!-- Adds the URL in the href attribute -->
Expand Down
4 changes: 4 additions & 0 deletions src/Demo/SafeRouting.Demo/Pages/Edit.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace SafeRouting.Demo.Pages;
Expand All @@ -6,6 +7,9 @@ namespace SafeRouting.Demo.Pages;

public sealed class EditModel : PageModel
{
[FromRoute]
public int Id { get; set; }

public void OnGet()
{
// ...
Expand Down
Loading

0 comments on commit d245d87

Please sign in to comment.