UrlActionGenerator 0.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package UrlActionGenerator --version 0.2.1
NuGet\Install-Package UrlActionGenerator -Version 0.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="UrlActionGenerator" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UrlActionGenerator --version 0.2.1
#r "nuget: UrlActionGenerator, 0.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install UrlActionGenerator as a Cake Addin
#addin nuget:?package=UrlActionGenerator&version=0.2.1

// Install UrlActionGenerator as a Cake Tool
#tool nuget:?package=UrlActionGenerator&version=0.2.1

UrlActionGenerator

NuGet Version Coverage Status

UrlActionGenerator is a C# Source Generator that assists the generation of URL's in ASP.NET Core projects and enforces the existance of a controller and method for the referenced URL.

What is UrlActionGenerator

UrlActionGenerator is a C# Source Generator that runs as part of the compilation process. Every time your project is being compiled, the compiler calls into UrlActionGenerator with the sources of your project. Using the same set of rules used by ASP.NET Core, UrlActionGenerator will figure out what all the controller classes are in your project and the actions.

Using the list of controllers and actions, some extra classes are generated as extension methods on the IUrlHelper class to generate type-safe methods for all controller actions in your project. These methods will contain the same parameters as your action methods so you can set them like you would be calling the method. The standard IUrlHelper.Action("ActionName", "ControllerName") method will then be used to generate the actual url, so if you use any custom [Route] or [HttpGet("url")] attributes, they will still work the same way as if you were using the IUrlHelper.Action method yourself.

How to use

To start using this package, simply add the UrlActionGenerator package to your ASP.NET Core project by using the IDE tooling or running the below command:

dotnet add package UrlActionGenerator

After that, your controllers and actions are automatically discovered and intellisense should almost directly be available in your controllers and views on the IUrlHelper.

UrlActionGenerator automatically generates extension methods on the IUrlHelper, the default method is Actions() which returns a class with properties for all the controllers in your project, those properties return another class with all the actions available in the controller and the parameters on those actions. If you use Area's in your project there are some extra extensions methods generated, one for each area. The name of those extension methods are {AreaName}Actions(), so for an "Admin" area it will be AdminActions().

Examples

Below are some examples on the methods that are generated in what scenario, how you can use it and what the result is. First off, a simple controller action without any parameters:

Controller:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Usage:

Url.Actions().Home.Index()

Result:

/Home/Index

A simple controller action with parameters and default parameter values

Controller:

public class HomeController : Controller
{
    public IActionResult Search(int page, int pageSize = 20)
    {
        return View();
    }
}

Usage:

Url.Actions().Home.Search(1);
Url.Actions().Home.Search(2, 50);

Result:

/Home/Search?page=1
/Home/Search?page=2&pageSize=50

Use with controllers in areas

Controller:

[Area("Admin")]
public class HomeController : Controller
{
    public IActionResult Index(string str)
    {
        return View();
    }
}

Usage:

Url.AdminActions().Home.Index("value");

Result:

/Admin/Home/Index?str=value
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.0 6,110 1/26/2022
0.5.0 273 12/21/2021
0.4.0 709 5/15/2021
0.4.0-alpha.6 153 5/10/2021
0.4.0-alpha.5 160 5/3/2021
0.4.0-alpha.4 143 5/2/2021
0.4.0-alpha.1 152 4/22/2021
0.3.0 361 3/14/2021
0.2.1 327 3/5/2021
0.2.0 332 3/5/2021
0.1.0 349 2/21/2021