Skip to content

Attributes and TagHelpers for loading static assets associated with ViewComponents

License

Notifications You must be signed in to change notification settings

BizStream/mvc-component-assets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mvc-component-assets License NuGet Version

Attributes and TagHelpers for rendering static assets (.js) associated with ViewComponents.

Usage

  • Install the AspNetCore package into the ASP.NET Core Mvc project:
dotnet add package BizStream.AspNetCore.ViewComponentAssets

OR

<PackageReference Include="BizStream.AspNetCore.ViewComponentAssets" Version="x.x.x" />
  • Configure services in the Startup.cs of the Mvc project:
using BizStream.AspNetCore.ViewComponentAssets;
using Microsoft.Extensions.DependencyInjection;

// ...

public void ConfigureServices( IServiceCollection services )
{
    services.AddMvc()

      // support ViewComponentAssets annotations/tag helpers
      .AddViewComponentAssets();
}

public void Configure( IApplicationBuilder app, IWebHostEnvironment environment )
{
    // ...
}
  • Annotate a ViewComponent:
using BizStream.AspNetCore.ViewComponentAssets.Annotations;
using Microsoft.AspNetCore.Mvc;

namespace MyComponents;

[ViewComponentScript("/scripts/my-view-component.js")]
public class MyViewComponent : ViewComponent
{
    public override IViewComponentResult Invoke( ) => Content( "Hello, World!" );
}
  • Use TagHelpers in Views:
@using MyComponents;
@addTagHelper *, BizStream.AspNetCore.ViewComponentAssets

<body>
  @(await Component.InvokeAsync<MyViewComponent>())

  <view-component-scripts />
</body>
  • Rendered HTML contains <script /> elements for ViewComponentScripts:
<body>
  Hello, World!  

  <script src="/scripts/my-view-component.js" />
</body>

About

Attributes and TagHelpers for loading static assets associated with ViewComponents

Resources

License

Stars

Watchers

Forks