Skip to main content

Posts

Showing posts matching the search for razor class library

Razor Class Libraries–Static Content

I’ll continue my journey using Razor Class Libraries in ASP.NET Core. Here are my previous posts: ASP.NET Core – Razor Class Libraries Razor Class Libraries -  Views not found Today I want to cover how you can use static content inside your Razor Class library. To include static content(images, fonts, stylesheets,…) in your RCL you need to create a wwwroot folder in the class library and include any required files there: When packing an RCL, all content in the wwwroot folder is automatically included in the package. As this content becomes part of the DLL you cannot just reference them from the root path(“~”. Instead the path is constructed using ‘_content/{LIBRARY NAME}/’. For example to reference an example.css file that you stored inside a RCL named ‘Example.RCL’, the correct way to include this css file in your application becomes:

Razor Class Libraries–Static Content 404 issue –Part 2

I’ll continue my journey using Razor Class Libraries in ASP.NET Core. Here are my previous posts: ASP.NET Core – Razor Class Libraries Razor Class Libraries -  Views not found Razor Class Libraries – Static Content Razor Class Libraries – Clean up your Content path Razor Class Libraries -  Static Content 404 issue – Part 1 After a first colleague returned to his desk with a solution for the problem I discussed yesterday , a second colleague arrived and explained he had a similar problem. This time I could pinpoint the issue to the following PackageReference that was (still) used in a referenced project: <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" /> Static files worked differently in .NET Core 2.2 Razor Class Libraries. The inclusion of the Microsoft.AspNetCore.Mvc v2.2.0 library broke the behaviour in ASP.NET Core 3.x applications. This reference is no longer needed as it is now a part of the...

Razor Class Libraries–Static Content 404 issue –Part 1

I’ll continue my journey using Razor Class Libraries in ASP.NET Core. Here are my previous posts: ASP.NET Core – Razor Class Libraries Razor Class Libraries -  Views not found Razor Class Libraries – Static Content Razor Class Libraries – Clean up your Content path Today I want to share an issue a colleague got when he tried to use a Razor Class Library I created. When he tried to load a static asset from the RCL, the asset was not found and a 404 error was returned to the browser. It took me a while to pinpoint the issue but in the end it turned out that the following setting in his ASP.NET Core project caused the problem: <ANCMPreConfiguredForIIS>true</ANCMPreconfiguredForIIS> After commenting out this line in the csproj file, the static assets were loaded correctly I have no clue why this solved the problem as I don’t see any relation between these features…

ASP.NET Core–Razor Class Libraries

A little known feature in ASP.NET Core (MVC/Razor pages) is the support for Razor Class libraries(RCL).  Through the RCL you can package and reuse UI components like View Components or Razor Components but also share full feature areas containing Razor view, controllers, models and so on… Let’s get started Open Visual Studio and choose Create a new project . Select Razor Class Library and click Next . Give the library a name and click on Create . Remark 1: To avoid a file name collision with the generated view library, ensure the library name doesn't end in .Views . Remark 2: Select Support pages and views if you need to support views. Otherwise only Razor components are supported. Now you can start adding your View Components and Razor components to your RCL. When building your RCL 2 DLL’s are created: One DLL containing all your logic One DLL containing your Razor views (ends with .Views ) You can now either reference this...

Razor Class libraries–Clean up your content path

I’ll continue my journey using Razor Class Libraries in ASP.NET Core. Here are my previous posts: ASP.NET Core – Razor Class Libraries Razor Class Libraries -  Views not found Razor Class Libraries – Static Content Today I want to write a small addition to my post from yesterday. As I mentioned yesterday to reference some content inside your razor views you need to use a path similar to _content/{LIBRARY NAME}. This path doesn’t look so nice. Luckily you change it to a different path name by editing the RCL project properties and adding a StaticWebAssetBasePath . Now you can access your files using /myownpath/example.css .

The role of ActivitySource in OpenTelemetry for .NET

While doing some pair programming to integrate OpenTelemetry tracing to a .NET application, we had a discussion on how to use the ActivitySource . It looks simple. You new one up, give it a name, start an activity, done. The discussion started when we added a second ActivitySource with the exact same name in a different class. This made us wonder: "Are we duplicating traces now? Is this a memory leak? Do we need a singleton?" So we decided to dig deeper. This post is what we learned… What ActivitySource actually is ActivitySource is part of System.Diagnostics , not part of the OpenTelemetry NuGet packages. Microsoft built tracing primitives directly into the BCL, and OpenTelemetry's .NET SDK simply listens to them. This is why you can add distributed tracing to a library without taking a dependency on OpenTelemetry at all. An ActivitySource is a factory for Activity objects, and an Activity is .NET's name for what OpenTelemetry calls a span.(don’t ask m...

Razor Class Libraries–Views not found

Last week I talked about Razor Class Libraries as a nice and easy way to package and share UI components for your ASP.NET Core MVC/Razor pages application. Inside my Razor Class Library I had some shared ViewComponents that I placed in a Shared folder. Unfortunately when trying to use these ViewComponents inside my ASP.NET Core Web application, the ViewComponents were not found?! It costed me some headaches before I discovered what I was doing wrong. It is really important that your shared View(Components) reside under a Views folder as this is where Razor will search it views by convention. You can override the convention if you want to but it is probably easier to just move everything to a Views folder like I did:

dotnet pack issue–files without extension

To share components, layout and some general CSS and javascript between ASP.NET Core MVC projects, we are using a Razor Class Library. This Razor Class library is packaged as a NuGet package and uploaded to Azure Artifacts. One of the libraries we share using this approach is Bootstrap. Similar to other open source projects, Bootstrap has an extensionless LICENSE file explaining the exact license agreements. When you package this project using dotnet pack , the extensionless LICENSE file is transformed into a ‘LICENSE’ folder container the ‘LICENSE’ file: I have no idea why this happens but OK it works…until the moment I added this NuGet package as a dependency to another project. When I try to publish this project (using dotnet publish ) it fails with the following error message: Error MSB3025: The source file "C:\Program Files\dotnet\sdk\6.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(237,5): C:\Users\tfsservice\packages\vlm.sofacore.web.huisstij...

Share reusable UI components through a Razor class library

One ASP.NET Core feature that I really like and most people are not aware of it existence are Razor Class libraries (RCL). With RCL you can package Razor views, pages, Razor components and view components and reuse them in different applications. I blogged about RCL before and although I really like the feature I got feedback from my development teams that the behavior was not consistent and they sometimes couldn’t get it working. I never had time to really investigate the root cause until now. So this blog post is here to explain my findings. The development environment First of all it is important to understand that the behavior of RCL is different in Development compared to other environments. When running the app that uses the RCL from build output ( dotnet run ), static web assets are enabled that allow ASP.NET Core to load static web assets from locations outside the wwwroot. This is required because ASP.NET Core will load the assets directly from the RCL NuGet location....