G4mvc 5.0.4-preview.104

This is a prerelease version of G4mvc.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package G4mvc --version 5.0.4-preview.104
                    
NuGet\Install-Package G4mvc -Version 5.0.4-preview.104
                    
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="G4mvc" Version="5.0.4-preview.104" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="G4mvc" Version="5.0.4-preview.104" />
                    
Directory.Packages.props
<PackageReference Include="G4mvc" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add G4mvc --version 5.0.4-preview.104
                    
#r "nuget: G4mvc, 5.0.4-preview.104"
                    
#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.
#:package G4mvc@5.0.4-preview.104
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=G4mvc&version=5.0.4-preview.104&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=G4mvc&version=5.0.4-preview.104&prerelease
                    
Install as a Cake Tool

G4mvc

G4mvc is a source generator for ASP.NET Core MVC apps that creates strongly typed helpers that eliminate the use of literal strings for routing.

It is an improved re-implementation of R4MVC using a C# Source Generator because R4MVC lacks support for many newer language - and MVC features.

CI

Installation

Install the G4mvc and G4mvc.Generator NuGet packages.

To enable the use of the tag helpers for anchor and form tags, add the @addTagHelper *, G4mvc directive either in the view, or in the _ViewImports.cshtml to enable them globally.

If you want to use the IUrlHelper and IHtmlHelper Extension methods, add a using directive for G4mvc.Extensions either in the view, or in the _ViewImports.cshtml to enable them globally.

It might be necessary to restart Visual Studio for these changes to take affect.

Use

Source Generation

Controllers

In order for the code generation to work, the controller class has to derive from the Microsoft.AspNetCore.Mvc.Controller class. Abstract classes will also be ignored. All Methods for which a routing helper should be generated, have to return Microsoft.AspNetCore.Mvc.IActionResult, Microsoft.AspNetCore.Mvc.Infrastructure.IConvertToActionResult or an implementation of either of these interfaces. For asyncronous controller actions, the task has to return one of these.

Examples:
public IActionResult Edit(EditViewModel viewModel)
public JsonResult Edit(EditViewModel viewModel)
public Task<IActionResult> Edit(EditViewModel viewModel)
public Task<JsonResult> Edit(EditViewModel viewModel)
public IConvertToActionResult Edit(EditViewModel viewModel)
public ActionResult<IEnumerable<string>> Edit(EditViewModel viewModel)

Something like public IEnumerable<string> Edit(EditViewModel viewModel) would be ignored.

To trigger the generation of the links class, it is necessary to manually build or rebuild the ASP.net core project or make change in the config file.
If you are using a StaticFilesPath different from the default wwwroot or add AdditionalStaticFilesPaths, you also have to add that path to the project file as AdditionalFiles.

<ItemGroup>
  <AdditionalFiles Include="mystaticfiles\**" />
</ItemGroup>

In case of ExcludedStaticFileExtensions or ExcludedStaticFileDirectories it's recommended to adjust the AdditionalFiles entry accordingly.

Extensions

The G4mvc package provides a number of extension methods that can make using the generated route helpers a bit easier. You do however not have to rely on these because the G4mvcRouteValues class derives from the standard Microsoft.AspNetCore.Routing.RouteValueDictionary, so you can use any of the methods provided by ASP.NET Core, that have an object routeValues parameter. An example would be the HtmlHelper.RouteLink Method. The provided extension methods are just wrappers for these methods.
For the generated G4mvcContentLink fields, you can use the IUrlHelper.Content extension method to get the virtual path.

Tag Helpers

g4-action

This tag helper can be used on anchor as well as form tags. It creates the href attribute for anchor tags or the action attribute for form tags.
<a g4-action="MVC.Home.Index()">Home</a>

g4-content

For static files, which are part of the generated Links, the g4-content tag helper can be applied to all html tags, that expect a file. It creates the attribute with the virtual path to the file.
<img g4-content="Links.images.sample_svg" />

Supported html tags:

  • embed
  • iframe
  • img
  • object
  • script
  • source
  • link
  • track
  • video

Configuration

You can provide a JSON config file called g4mvc.json to change some of the defaults G4mvc uses.
The json schema is available under https://schemastore.af-styx.com/Schema/G4mvc.json (alternative: https://raw.githubusercontent.com/andref15/G4mvc/main/Json/schema.json)

{
  "$schema": "https://schemastore.af-styx.com/Schema/G4mvc.json",
  "HelperClassName": "MVC",
  "LinksClassName":  "Links",
  "StaticFilesPath": "wwwroot",
  "UseVirtualPathProcessor": false,
  "UseProcessedPathForContentLink": false,
  "MakeGeneratedClassesInternal": false,
  "GeneratedClassNamespace": "global",
  "EnableSubfoldersInViews": false,
  "ExcludedStaticFileExtensions": [],
  "ExcludedStaticFileDirectories": [],
  "AdditionalStaticFilesPaths": {},
  "CustomStaticFileDirectoryAlias": {}
}
HelperClassName

Allows you to change the MVC prefix (e.g. MVC.Home.Index()). Default is MVC.

LinksClassName

Allows you to chnage the name of the class in which the links for static files are generated in. Default is Links.

StaticFilesPath

The root path (relative to project dir) for which links will be generated. Default is wwwroot.

UseVirtualPathProcessor

Defines if you want to define a custom VirtualPathProcessor funcion. By default this is false. When this is set to true, a partial class VirtualPathProcessor with a partial method Process will be generated and you have to write the implementation of this partial method. An example of this can be seen here:

internal static partial class VirtualPathProcessor
{
    public static partial string Process(string path)
    {
        return path.ToUpper();
    }
}

Defines if the processed path from the VirtualPathProcessor should be used for UrlHelper.Content methods and the g4-content tag helper.
If this value is undefined and UseVirtualPathProcessor is set to true, this defaults to true.

MakeGeneratedClassesInternal

Defines if the generated route classes and the MVC and Links class will be public or internal. Default is false.

GeneratedClassNamespace

Defines in which namespace the generated MVC and Links class will exist. If this value is undefined, this defaults to global

  • global defines that the files will exist in the global namespace (aka no namespace)'
  • project defines that the files will exist in the root namespace of the web project
  • any other value will be set as the namespace (eg. G4mvc.SampleMVC)
    • If the value starts with a . the namespace will be relative to the project root namespace (e.g. .Generated)
EnableSubfoldersInViews

Defines if subfolders in the controller's Views folder should be supported. Default is false.

ExcludedStaticFileExtensions

A list of file extensions that will be excluded from the Links generation.

ExcludedStaticFileDirectories

A list of directories (relative to project dir) that will be excluded from the Links generation. This can be useful if you, for example, want to exclude a directory of a library, because you never reference these files directly.

AdditionalStaticFilesPaths

A dictionary of additional static file paths for which links will be generated. This is useful when you use the UseStaticFiles method to serve files that are outside of the wwwroot folder. The key of this dictionary is the physical path relative to the project root and the value is the request path.

For the example provided in the Microsoft Documentation the AdditionalStaticFilesPaths configuration would look like this:

"AdditionalStaticFilesPaths": {
    "MyStaticFiles": "StaticFiles"
}
CustomStaticFileDirectoryAlias

A dictionary of aliases for certain directories (relative to project dir). This can be useful if the sanitized names of two or more subdirectories in a directory are the same and renaming them is not an option. E.g. if you had a directory called some-directory and another one called some.directory in the wwwroot directory, the sanitized name of both of these would be some_directory, therefore the same class name would be generated twice. To fix the situation described, a possible configuration would be the following:

"CustomStaticFileDirectoryAlias": {
    "wwwroot/some.directory": "SomeDotDirectory"
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on G4mvc:

Package Downloads
G4mvc.Generator

A source generator for ASP.NET Core MVC apps that creates strongly typed helpers that eliminate the use of literal strings for routing

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.0.0-beta.supportforrazorp... 114 6/25/2025
6.0.0-beta.supportforrazorp... 110 6/25/2025
6.0.0-beta.supportforrazorp... 109 6/25/2025
6.0.0-beta.supportforrazorp... 124 6/23/2025
5.0.4-preview.104 55 7/11/2025
5.0.4-beta.issuewithusinggm... 42 7/11/2025
5.0.4-beta.issuewithusinggm... 46 7/11/2025
5.0.4-beta.issuewithusinggm... 114 7/8/2025
5.0.4-beta.issuewithgconten... 51 7/11/2025
5.0.4-beta.issuewithgconten... 45 7/11/2025
5.0.3 156 7/8/2025
5.0.3-preview.94 70 7/5/2025
5.0.3-beta.issuewithusinggm... 117 7/8/2025
5.0.3-beta.generatelinksonr... 107 7/6/2025
5.0.3-beta.generatelinksonr... 42 7/5/2025
5.0.3-beta.generatelinksonr... 46 7/5/2025
5.0.3-beta.generatelinksonr... 42 7/5/2025
5.0.3-beta.generatelinksonr... 47 7/5/2025
5.0.3-beta.generatedviewfil... 75 7/4/2025
5.0.2 209 3/17/2025
5.0.1-preview.76 126 3/16/2025
5.0.1-preview.70 130 3/13/2025
5.0.1-beta.removeoldslnfile.69 123 3/13/2025
5.0.1-beta.removenetstandar... 122 3/16/2025
5.0.1-beta.removenetstandar... 127 3/13/2025
5.0.0 153 11/14/2024
5.0.0-beta.slntoslnx.55 69 11/9/2024
5.0.0-beta.preview.59 55 11/14/2024
5.0.0-beta.net.61 54 11/14/2024
5.0.0-beta.net.58 69 11/10/2024
5.0.0-beta.net.57 60 11/10/2024
4.1.2 172 8/8/2024
4.1.2-beta.slntoslnx.52 63 11/9/2024
4.1.2-beta.readmeismissingn... 62 8/8/2024
4.1.2-beta.net.54 66 11/9/2024
4.1.1 175 5/23/2024
4.1.1-linkgenerationforsubf... 79 5/23/2024
4.1.1-linkgenerationforsubf... 69 5/23/2024
4.1.1-beta.readmeismissingn... 65 7/2/2024
4.1.1-beta.preview.45 65 6/12/2024
4.1.0 177 5/5/2024
4.1.0-preview.19 38 5/2/2024
4.1.0-preview.18 35 5/2/2024
4.1.0-preview.17 31 5/2/2024
4.1.0-preview.11 92 2/5/2024
4.1.0-preview.10 85 2/2/2024
4.1.0-missingcommasindocume... 69 5/8/2024
4.1.0-linksgenerationimprov... 77 5/5/2024
4.1.0-linksgenerationimprov... 75 5/5/2024
4.1.0-linkgenerationforsubf... 70 5/23/2024
4.1.0-10-links-generation-i... 73 5/5/2024
4.1.0-10-links-generation-i... 73 5/4/2024
4.1.0-10-links-generation-i... 72 5/4/2024
4.1.0-10-links-generation-i... 37 5/2/2024
4.1.0-10-links-generation-i... 41 5/2/2024
4.1.0-10-links-generation-i... 35 5/2/2024
4.1.0-10-links-generation-i... 69 4/19/2024
4.1.0-10-links-generation-i... 71 4/19/2024
4.0.0 274 1/4/2024
4.0.0-preview.12 73 3/20/2024
4.0.0-preview.9 67 2/2/2024
4.0.0-preview.8 72 2/2/2024
3.0.0 2,076 9/25/2023
3.0.0-preview.21 89 1/4/2024
3.0.0-preview.3 91 1/4/2024
3.0.0-preview.2 84 1/4/2024
3.0.0-preview.1.23285.2 88 10/12/2023
3.0.0-preview.1.23285.1 85 10/12/2023
3.0.0-preview.1.23230.3 97 8/18/2023
2.3.0 288 7/16/2023
2.2.1-preview.1.23197.3 113 7/16/2023
2.2.1-preview.1.23197.2 109 7/16/2023
2.2.1-preview.1.23197.1 111 7/16/2023
2.2.1-preview.1.23195.4 115 7/14/2023
2.2.1-preview.1.23195.3 113 7/14/2023
2.2.1-preview.1.23195.2 108 7/14/2023
2.2.1-preview.1.23195.1 113 7/14/2023
2.2.0 271 7/11/2023
2.1.2-preview.1.23192.1 109 7/11/2023
2.1.1 276 6/18/2023
2.1.1-preview.1.23169.3 104 6/18/2023
2.1.1-preview.1.23169.2 103 6/18/2023
2.1.1-preview.1.23169.1 106 6/18/2023
2.1.1-preview.1.23166.10 110 6/15/2023
2.1.1-preview.1.23166.9 110 6/15/2023
2.1.1-preview.1.23166.8 106 6/15/2023
2.1.1-preview.1.23166.7 106 6/15/2023
2.1.1-preview.1.23166.5 113 6/15/2023
2.1.0 1,044 6/1/2023
2.0.0 278 5/13/2023
1.1.2 761 3/5/2023
1.1.1 492 2/25/2023
1.1.0 485 2/9/2023
1.0.5 620 1/2/2023
1.0.4 649 11/21/2022
1.0.3 700 11/21/2022 1.0.3 is deprecated because it has critical bugs.
1.0.2 658 11/21/2022 1.0.2 is deprecated because it has critical bugs.
1.0.1 645 11/17/2022
1.0.0 813 10/9/2022
1.0.0-preview.1.22282.6 140 10/9/2022
1.0.0-preview.1.22253.6 203 9/10/2022
1.0.0-preview.1.22243.5 168 8/31/2022