RSCG – EndpointHelpers

 
 

name EndpointHelpers
nuget https://www.nuget.org/packages/EndpointHelpers/
link https://github.com/gumbarros/EndpointHelpers
author Gustavo Mauricio de Barros

Generating endpoint helpers for ASP.NET Core MVC

 

This is how you can use EndpointHelpers .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="EndpointHelpers" Version="2.0.2" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is



var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

//app.UseHttpsRedirection();
app.UseRouting();

app.UseAuthorization();

app.MapStaticAssets();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}")
    .WithStaticAssets();


app.Run();



using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using TestMVC.Models;
using EndpointHelpers;

namespace TestMVC.Controllers;

[GenerateUrlHelper]
[GenerateLinkGenerator]
[GenerateRedirectToAction]
[GenerateViewHelpers]
public partial class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Privacy()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}



@using EndpointHelpers;

@{
    ViewData["Title"] = "Privacy Policy";

}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>


<a href="@Url.Home.Index()"> This is the index referenced directly</a>


 

The code that is generated is

// <auto-generated/>
#nullable enable

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace EndpointHelpers;

public sealed class HomeControllerLinkGenerator(LinkGenerator linkGenerator)
{
    public string GetIndexPath()
    {
        return linkGenerator.GetPathByAction(
            action: "Index",
            controller: "Home",
            values: null)!;
    }

    public string GetIndexPath(HttpContext httpContext)
    {
        return linkGenerator.GetPathByAction(
            httpContext,
            action: "Index",
            controller: "Home",
            values: null)!;
    }

    public string GetPrivacyPath()
    {
        return linkGenerator.GetPathByAction(
            action: "Privacy",
            controller: "Home",
            values: null)!;
    }

    public string GetPrivacyPath(HttpContext httpContext)
    {
        return linkGenerator.GetPathByAction(
            httpContext,
            action: "Privacy",
            controller: "Home",
            values: null)!;
    }

    public string GetErrorPath()
    {
        return linkGenerator.GetPathByAction(
            action: "Error",
            controller: "Home",
            values: null)!;
    }

    public string GetErrorPath(HttpContext httpContext)
    {
        return linkGenerator.GetPathByAction(
            httpContext,
            action: "Error",
            controller: "Home",
            values: null)!;
    }

}

public static class LinkGeneratorExtensions
{
    extension(LinkGenerator linkGenerator)
    {
        public HomeControllerLinkGenerator Home => new HomeControllerLinkGenerator(linkGenerator);

    }
}

// <auto-generated/>
#nullable enable

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;

namespace TestMVC.Controllers
{
    public partial class HomeController
    {
        protected RedirectToActionResult RedirectToIndex()
        {
            return RedirectToAction("Index", "Home")!;
        }

        protected RedirectToActionResult RedirectToPrivacy()
        {
            return RedirectToAction("Privacy", "Home")!;
        }

        protected RedirectToActionResult RedirectToError()
        {
            return RedirectToAction("Error", "Home")!;
        }

    }

}


// <auto-generated/>

namespace EndpointHelpers;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(
    global::System.AttributeTargets.Method |
    global::System.AttributeTargets.Class |
    global::System.AttributeTargets.Assembly)]
internal sealed class GenerateUrlHelperAttribute : global::System.Attribute;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(global::System.AttributeTargets.Method)]
internal sealed class UrlHelperIgnoreAttribute : global::System.Attribute;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(
    global::System.AttributeTargets.Method |
    global::System.AttributeTargets.Class |
    global::System.AttributeTargets.Assembly)]
internal sealed class GenerateLinkGeneratorAttribute : global::System.Attribute;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(global::System.AttributeTargets.Method)]
internal sealed class LinkGeneratorIgnoreAttribute : global::System.Attribute;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(
    global::System.AttributeTargets.Method |
    global::System.AttributeTargets.Class |
    global::System.AttributeTargets.Assembly)]
internal sealed class GenerateRedirectToActionAttribute : global::System.Attribute;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(global::System.AttributeTargets.Method)]
internal sealed class RedirectToActionIgnoreAttribute : global::System.Attribute;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(global::System.AttributeTargets.Class)]
internal sealed class GenerateViewHelpersAttribute : global::System.Attribute;

[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
[global::System.AttributeUsage(global::System.AttributeTargets.Method)]
internal sealed class ViewHelpersIgnoreAttribute : global::System.Attribute;
 
// <auto-generated/>
namespace Microsoft.CodeAnalysis
{
    internal sealed partial class EmbeddedAttribute : global::System.Attribute
    {
    }
}
// <auto-generated/>
#nullable enable
using Microsoft.AspNetCore.Mvc;

namespace EndpointHelpers;

public sealed class HomeControllerUrlHelper(IUrlHelper url)
{
    public string Index()
    {
        return url.Action("Index", "Home")!;
    }

    public string Privacy()
    {
        return url.Action("Privacy", "Home")!;
    }

    public string Error()
    {
        return url.Action("Error", "Home")!;
    }

}

public static class UrlHelperExtensions
{
    extension(IUrlHelper url)
    {
        public HomeControllerUrlHelper Home => new HomeControllerUrlHelper(url);

    }
}

// <auto-generated/>
#nullable enable

using Microsoft.AspNetCore.Mvc;

namespace TestMVC.Controllers
{
    public partial class HomeController
    {
        protected ViewResult IndexView()
        {
            return new ViewResult
            {
                ViewName = "Index",
                ViewData = ViewData,
                TempData = TempData
            };
        }

        protected ViewResult IndexView(object? model)
        {
            ViewData.Model = model;
            return new ViewResult
            {
                ViewName = "Index",
                ViewData = ViewData,
                TempData = TempData
            };
        }

        protected ViewResult PrivacyView()
        {
            return new ViewResult
            {
                ViewName = "Privacy",
                ViewData = ViewData,
                TempData = TempData
            };
        }

        protected ViewResult PrivacyView(object? model)
        {
            ViewData.Model = model;
            return new ViewResult
            {
                ViewName = "Privacy",
                ViewData = ViewData,
                TempData = TempData
            };
        }

    }

}


Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/EndpointHelpers


Posted

in

,

by

Tags: