Can Razor Class Library Pack Static Files (Js, CSS etc) Too

Can Razor Class Library pack static files (js, css etc) too?

Ehsan answer was correct at the time of asking (for .NET Core 2.2), for .NET Core 3.0 onwards (including .NET 7 when I update this link), RCL can include static assets without much effort:

To include companion assets as part of an RCL, create a wwwroot folder in the class library and include any required files in that folder.

When packing an RCL, all companion assets in the wwwroot folder are automatically included in the package.

The files included in the wwwroot folder of the RCL are exposed to the consuming app under the prefix _content/{LIBRARY NAME}/. For example, a library named Razor.Class.Lib results in a path to static content at _content/Razor.Class.Lib/.

404 with static content in Razor Class Library (RCL)

If you remove the reference to Microsoft.AspNet.Core from the RclDemo.Library project, then everything works as expected. This is the line that should be removed from the project file:

<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />

If you create a new .NET Core 3.x application, you would not include this reference as it is now included as part of the Microsoft.AspNetCore.App framework reference. Static files worked differently in .NET Core 2.2 Razor Class Libraries, and I think your inclusion of the Microsoft.AspNetCore.Mvc v2.2.0 library is breaking things. There used to be additional configuration to include static files in an RCL.

Shawn

BlazorWebAssembly 5.0 does not load static assets from Razor class library

You also have to: right click on the Dependencies folder, and select Add Project Reference..., and then tick the check-box for RazorClassLibrary1

Razor WebApp doesn't load css when plugged as middleware to WebApi project

So, after some days of investigation, I found this question.
Can Razor Class Library pack static files (js, css etc) too?

Ehsan Mirsaeedi provided a working way to embed static files into RCL and it works for my project. Here is a link to answer https://stackoverflow.com/a/53024912.

What is difference class library and razor class library?

A Razor Class Library is use specifically for creating a set of reusable User Interface widgets that you can plug-in to multiple web apps

A Class Library is a set of usually non UI related classes, to implement a particular function/service or api. e.g. Most of the .Net libraries we use are class libraries.

Using StaticFileOptions() breaks embedded static files from Razor Class Library

The static files middleware can receive its StaticFileOptions in one of two ways:

  1. Implicitly via Dependency Injection.
  2. Explicitly via the call to UseStaticFiles.

In your problematic scenario, you're (inadvertently) attempting to configure the middleware using both of these approaches. As soon as you add an argument to the UseStaticFiles call, you're replacing the framework-provided configuration for the middleware, which includes setup for RCL support.

To build on the framework-provided configuration, you can take advantage of the options pattern in ConfigureServices:

services.Configure<StaticFileOptions>(options =>
{
options.OnPrepareResponse = ctx =>
{
const int durationInSeconds = 60 * 60 * 24;
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
"public, max-age=" + durationInSeconds;
}
});

You'll also need to remove the argument being passed to UseStaticFiles.

is it possible to using razor class library for all web projects?

is it possible to write all core or data layer code inside different
razor class library

Please check the official document and this tutorial:

The Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL). The RCL can be packaged and reused. Applications can include the RCL and override the views and pages it contains. When a view, partial view, or Razor Page is found in both the web app and the RCL, the Razor markup (.cshtml file) in the web app takes precedence.

So, you can write the Data layer code or logic in the Razor class library. But in my opinion, I suggest you could put the data layer code in the regular class library, then you could reuse them in other projects.



Related Topics



Leave a reply



Submit