dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.45k stars 10.03k forks source link

Static assets in Razor libraries do not seem to work in Blazor server-side applications #11410

Closed akorchev closed 5 years ago

akorchev commented 5 years ago

If you believe you have an issue that affects the security of the platform please do NOT create an issue and instead email your issue details to secure@microsoft.com. Your report may be eligible for our bug bounty but ONLY if it is reported through email.

Describe the bug

The approach described here and here does not seem to work in server-side Blazor applications.

To Reproduce

Steps to reproduce the behavior:

  1. dotnet new blazorserverside -o BlazorWebApp1 (a change from the preview 6 announcement blog post)
  2. dotnet new razorclasslib -o RazorLib1
  3. dotnet add BlazorWebApp1 reference RazorLib1
  4. Add a CSS file in RazorLib1/wwwroot/ e.g. styles.css
  5. Reference that css style in BlazorWebApp1 by appending <link rel="stylesheet" href="_content/RazorLib1/styles.css"> to _Host.cshtml
  6. dotnet run
  7. The _content/RazorLib1/styles.css HTTP requests has 404 status.

My test application is available here.

Expected behavior

I would expect for this to work as it seems to should have been a supported scenario.

Screenshots

If applicable, add screenshots to help explain your problem.

image

Additional context

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview6-012264
 Commit:    be3f0c1a03

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.13-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.0.100-preview6-012264/

Host (useful for support):
  Version: 3.0.0-preview6-27804-01
  Commit:  fdf81c6faf
huanbd commented 5 years ago

I think, I have the same error

.NET Core SDK version: 3.0.100-preview6-012264
OS Name: Window 10
OS Version: 1903

I have a project Blazor client. It is configured as a page in a project Asp.net Core MVC

app.Map("/cms", appBuilder =>
{
     app.UseClientSideBlazorFiles<Admin.Startup>();
     app.UseEndpoints(endpoints =>
     {
           endpoints.MapFallbackToClientSideBlazor<Admin.Startup>("index.html");
     });
 });

In index.html file <base href="/cms/" />

/cms/css/site.css not found ...

It run without error in SDK version preview5

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @akorchev. @javiercn can you please look into this? Thanks!

javiercn commented 5 years ago

@huanbd Your issue is likely unrelated.

If you have a base path then you need to reference files relative to the root (starting with '/' or call app.UseStaticFiles() within the app.Map("/cms", ...) call before app.UseClientSideBlazorFiles

huanbd commented 5 years ago

Hi @javiercn , I've used app.UseStaticFiles() before that, this is my Configuremethod in Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBlazorDebugging();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.Map("/cms", appBuilder =>
    {
        app.UseClientSideBlazorFiles<Admin.Startup>();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapFallbackToClientSideBlazor<Admin.Startup>("index.html");
        });
    });
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

May I miss something about api change?

javiercn commented 5 years ago
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBlazorDebugging();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();
    app.Map("/cms", appBuilder =>
    {
        // You either add this call or reference the file as /css/site.css as the physical path on disk for the file I imagine is /css/site.css
        app.UseStaticFiles();
        app.UseClientSideBlazorFiles<Admin.Startup>();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapFallbackToClientSideBlazor<Admin.Startup>("index.html");
        });
    });
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}
huanbd commented 5 years ago

I did that but still error, and note that Client side Blazor is a project, that's outside of project MVC, and static files are in project client side Blazor and in version SDK preview 5 It run without error

Capture

javiercn commented 5 years ago

@huanbd Your issue is unrelated to this issue. I believe it might have been related to an issue we fixed a couple of days ago. I would suggest you try out with the next preview release or file a separate issue and include a repro project.

javiercn commented 5 years ago

@akorchev I'm not able to repro the issue with your project and the latest sdk. Could you provide what sdk you are using? We have automation covering the scenario you describe, so I'm inclined to think that the razor sdk in use might be old.

Could you please try with the latest sdk from https://github.com/dotnet/core-sdk and see if you still have this issue?

Closing for now as no-repro

akorchev commented 5 years ago

@javiercn I think I am using the latest SDK:

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview6-012264
 Commit:    be3f0c1a03

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.13-x64
 Base Path:   /usr/local/share/dotnet/sdk/3.0.100-preview6-012264/

Host (useful for support):
  Version: 3.0.0-preview6-27804-01
  Commit:  fdf81c6faf