Skip to content

Where is manifest && works with controllers/views but not static content - CSS #16837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DominikAmon opened this issue Feb 4, 2020 — with docs.microsoft.com · 11 comments
Assignees
Labels
doc-enhancement needs-more-info Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

Copy link

DominikAmon commented Feb 4, 2020

I could get the example working with Controllers and Views, but not with static content, such as CSS. It would be great to improve the existing sample code on github with static content exmaples.
Regarding: "When the RCL is built, a manifest is produced that describes the static web asset locations." - How can I verify this / Where to find it?

I posted already a message on Stackoverflow:
404 with static content in Razor Class Library (RCL) - StackOverflow


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@dotnet-bot dotnet-bot added ⌚ Not Triaged Source - Docs.ms Docs Customer feedback via GitHub Issue labels Feb 4, 2020
@Rick-Anderson Rick-Anderson changed the title Troubleshooting Where is manifest && works with controllers/views but not static content - CSS Feb 4, 2020
@Rick-Anderson
Copy link
Contributor

@DominikAmon can you create the simplest possible solution (RCL + consuming app) that demonstrates this problem? Post the GitHub link here.

Did you follow Create an RCL with static assets?

"When the RCL is built, a manifest is produced that describes the static web asset locations."

@pranavkm how do we access the manifest?

@pranavkm
Copy link
Contributor

pranavkm commented Feb 4, 2020

It's an implementation detail so I wouldn't necessarily rely on using it in any way outside of what the framework does. But if you'd like to inspect it's contents, it's an xml default that's produced in the obj\Debug\netstandard2.1\staticwebassets directory.

@DominikAmon
Copy link
Author

@Rick-Anderson I created a small sample app and published it on GitHub https://github.com/DominikAmon/RclIssueDemo/

I clearly made something wrong, but it is hard to figure out, as I am not getting any details other than 404.

@Rick-Anderson
Copy link
Contributor

@Rick-Anderson
Copy link
Contributor

I placed a css file in the library at the following location: wwwroot\css\Base.css and I tested the following path: https://localhost:44300/_content/OurIt.Cockpit/css/Base.css,

That's not the right URL.

Create a web app, put a static resource in wwwroot, and access it.

@Rick-Anderson
Copy link
Contributor

I could get the example working with Controllers and Views, but not with static content, such as CSS

The views are pulling static assets. You're just using the wrong path.

@cesarsouza
Copy link
Contributor

I could get the example working with Controllers and Views, but not with static content, such as CSS

The views are pulling static assets. You're just using the wrong path.

What would be the correct path? I am afraid I am facing the same issue, but not even the MVC views are being found.

I am also getting many CSC : warning AD0001: Analyzer 'Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. as soon as I build the consumer app, but I may report this in another issue.

@cesarsouza
Copy link
Contributor

cesarsouza commented May 9, 2020

Nevermind, I finally got it working. I am not completely sure yet what did the trick for me, but I had to configure my RCL project differently from what is shown in the demonstration code from the docs. Maybe those examples need to be updated.

The relevant parts of my now working .csproj are:

<Project Sdk="Microsoft.NET.Sdk.Razor">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
    <GenerateMvcApplicationPartsAssemblyAttributes>true</GenerateMvcApplicationPartsAssemblyAttributes>
    <RazorCompileOnBuild>true</RazorCompileOnBuild>
    <IncludeRazorContentInPack>false</IncludeRazorContentInPack>
    <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
    <EnableDefaultRazorGenerateItems>true</EnableDefaultRazorGenerateItems>
  </PropertyGroup>

 <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
  </ItemGroup>

  <PropertyGroup>
    <StaticWebAssetBasePath Condition="$(StaticWebAssetBasePath) == ''">/</StaticWebAssetBasePath>
  </PropertyGroup>
</Project>

Maybe not all of those settings above are required, but since it is working, I do not want to touch it too much 😁

Now, here are other issues that were getting in the way:

  • Because I wanted to test my NuGet packages locally, I wanted to first push the packages to a local repository folder so I could consume them from a test application. However, no where it is documented that dotnet nuget can also push to local directories, so I thought I would have to use the standalone nuget.exe to achieve this. Well, apparently it is not a good idea to use nuget.exe with RCL libraries. Instead, you can use:
dotnet nuget push .\bin\Release\DynamicVML.1.0.32.nupkg -s c:\Projects\nuget

(note that the -s option is described as a server URL in the documentation but it can actually be a local folder, and this is not explained)

  • There are so many outdated articles and StackOverflow answers around that could set you up in the wrong path. What happened with me was that at some point I was trying to follow tutorials from different sources which turned out to be outdated for .NET Core 3.1. I ended up messing a little my project files and forgot to undo some of the changes I had made. To get it all working, make sure that:

  • The views / .cshtml files have to be set to "Build Action: Content";

  • The static files / js files have to be set to "Build Action: Content";

Now, to the consumer application part:

  • I did not have to add webBuilder.UseStaticWebAssets(); in Program.cs as for a moment I thought I had. So my CreateHostBuilder simply looks like this:
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

I did not have to add any of the FileProvider functionality that is described in the outdated tutorials. My ConfigureServices is as clean as this:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseInMemoryDatabase("BookAuthors"));
}

and my Configure is just the baseline one:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        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.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

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

With the above settings, everything works. I can include js files from the RCL library even though they are not physically present on the consumer project wwwroot (I know that this is documented in the current documentation, but at some point I was trying to debug the issue by adding a UseDirectoryBrowser to my configure and inspecting the contents of the served folders - this does not work, the static files from the RCL will not show up).

Also, here is a note for others with similar issues: with the above settings (due the last configuration block, more specifically) I can consume the static files from /. I am not using the /_content/LibraryName/... paths. In my RCL, I have the following structure

- wwwroot
---- lib
------- myFolder
---------- myScript.js

From the consumer app, I simply consume the .js file using

@section Scripts {
    <script src="~/lib/myFolder/myScript.js"></script>
}

@Rick-Anderson
Copy link
Contributor

@cesarsouza thanks for the update.

@mkalinski93
Copy link

@cesarsouza I followed your instructions, but have you tried running multiple RCL´s simultaneously?
In my case, I have had two rcl´s with the exact same configurations.
After building, I´ve received the following errors:

"Duplicate base paths '/' for content root paths 'projectA' 'projectB'

@cesarsouza
Copy link
Contributor

@cesarsouza I followed your instructions, but have you tried running multiple RCL´s simultaneously?
In my case, I have had two rcl´s with the exact same configurations.
After building, I´ve received the following errors:

"Duplicate base paths '/' for content root paths 'projectA' 'projectB'

Hi @mkalinski93,

Not at all. To be honest I had only tested with a library I built mylsef (dynamic view model lists / dvml / dynamic-vml.github.io/ )

I don't know what would happen when multiple libraries would have been loaded at the same time.

César

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement needs-more-info Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

No branches or pull requests

6 participants