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.36k stars 9.99k forks source link

Static assets request path problems in preview6 #11174

Closed AlseinX closed 5 years ago

AlseinX commented 5 years ago

Describe the bug

In preview 6 when there is a wwwroot folder in a blazor library project, the request path of server side and client side are different.

To Reproduce

Steps to reproduce the behavior:

  1. dotnet new a blazor library project with a name that contains ..
  2. Rename the content folder to wwwroot to make use of the new static assets feature add in preview6.
  3. Change the references in the csproj file of the library project to use wwwroot instead of content (There would even not be any proper url path to the assets with client side hosting if this step is skipped. Question: Why doesn't blazor client side use the new wwwroot support? )
  4. dotnet new a blazor client side and a blazor server side project that reference the library project.
  5. Run and test both project.

Expected behavior

Static assets in both project can be accessed with the same url path.

Actual behavior

The file "background.png" is accessed with different url path: Client side hosting: https://<hostname>/_content/<TheOriginal.LibraryName>/background.png Server side hosting: https://<hostname>/_content/<lowercaselibrarynamewithoutdots>/background.png

Consequence

A blazor library with static assets cannot have a name with ., otherwise it would have different behavior when referenced by client side and server side hostings.

Is there any plan to fix this fault in the futural preview, or some quick fix for the current version?

karthickthangasamy commented 5 years ago

I am also facing this issue with dot name library application. The wwwroot assets are not loaded in server-side Blazor application.

image

The application is working as expected while renaming the library app into RazorLib1. Could you please provide some quick fix for this issue.?

AlseinX commented 5 years ago

I am also facing this issue with dot name library application. The wwwroot assets are not loaded in server-side Blazor application.

image

The application is working as expected while renaming the library app into RazorLib1. Could you please provide some quick fix for this issue.?

You can access by replacing the Test.RazorLib1 to testrazorlib1 from the url and it should work on server side hosting, but it stops working on client side hosting. :-<

karthickthangasamy commented 5 years ago

@AlseinX : Yes, it will work, but we need generic solution for all the hosting models of Blazor.

AlseinX commented 5 years ago

@karthickthangasamy Me too.

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @AlseinX. @javiercn can you please look into this? Seems like something we'll need to normalize on.

javiercn commented 5 years ago

It is expected that blazor client-side will also move to use _content/<> we just have been focusing on other work.

mikes-gh commented 5 years ago

@javiercn

You can access by replacing the Test.RazorLib1 to testrazorlib1

Struggled with this as didn't know I had to replace the dotted project name with lowercase without dot. AFAIK dots are allowed in the path part of a URL. Also case is ignored in URLs

Whats the reason for this

Unlikely but if I had 2 projects of same name but only differing by dot this would break.

Project.Test ProjectTest

javiercn commented 5 years ago

@mikes-gh dots are allowed, but normally cause issues, with many proxies and redirection rules, because people write simple rules like . to refer to files and that causes problems.

Urls are case-sensitive. Many static files implementations simply rely on the file system casing, that's why in some cases they can seem case insensitive, and you can choose to interpret them that way, but in HTTP they are case sensitive, if not, try to set a cookie path to /PATH and visit the site at /path and you'll see how the browser doesn't send the cookie.

mikes-gh commented 5 years ago

@javiercn thanks so is the plan to use lowercase no dots for both client and server.

javiercn commented 5 years ago

@mikes-gh yes

mikes-gh commented 5 years ago

@javiercn can I ask why the _content prefix for this feature? It doesn't seem very intuitive to me.. Maybe the static path prefix could be made configurable in the csproj of the library Whilst convention is good to keep code down in this case it may be less confusing to state the path particularly because the rule is a bit awkward. Just my 2c and I am sure you have lots of other priorities :smile:

mikes-gh commented 5 years ago

Another thought I had which may make more sence naming wise is to replace dots with hyphens. _content/Blazor.Lib. becomes _content/blazor-lib seems a bit better to me and looking at conventional js libraries this seems to be a common pattern

javiercn commented 5 years ago

@mikes-gh We use _content by default to clearly distinguish what's directly from your app with what comes through static web assets, as a safe way to avoid collisions and keep everything under a common subspace of the url address space.

mikes-gh commented 5 years ago

@javiercn any thoughts on replacing dots with hyphens instead of removing them e.g. _content/Blazor.Lib. becomes _content/blazor-lib

javiercn commented 5 years ago

We are planing to go back to _content/$(PackageId) as the default convention.

mikes-gh commented 5 years ago

@javiercn - I agree with your desision. At the moment the docs say _content/{LIBRARY NAME}/

Does LIBRARY NAME mean the csproj name?

I think its possible to use a RCL from a project reference that doesn't include a <PackageId> i.e. an RCL that has never been packed? This might happen when testing.

Forgive me if I'm missing something just coming at this from an end-user perspective.

javiercn commented 5 years ago

$(PackageId) is always defined, even if you don't pack your project. It will work whether you pack the project or reference it

javiercn commented 5 years ago

Fixed in https://github.com/aspnet/AspNetCore-Tooling/commit/98b168c8947d8c7f7b3f2beb3a0561fb89cadfb5

This will be in preview7

AlseinX commented 5 years ago

@javiercn And another problem is, making a blazor component library that supports client-side hosting still needs specifying the wwwroot folder instead of content (which is in the dotnet new blazorlib template) manually while the server-side support takes `wwwroot as default name convention.

<EmbeddedResource Include="wwwroot\**\*.js" LogicalName="blazor:js:%(RecursiveDir)%(Filename)%(Extension)" />

<EmbeddedResource Include="wwwroot\**\*.css" LogicalName="blazor:css:%(RecursiveDir)%(Filename)%(Extension)" />

<EmbeddedResource Include="wwwroot\**" Exclude="**\*.js;**\*.css" LogicalName="blazor:file:%(RecursiveDir)%(Filename)%(Extension)" />

Is it planned to make client-side and server-side use the same way shipping static assets?

Additionally, the static assets is embedded as resources and also copied to the dist _content/... folders when publishing a client-side hosted blazor application, does it mean the browser need to download static assets twice, for once directly via the _content/... path and once within the dll?

javiercn commented 5 years ago

dotnet blazorlib is going away in favor of simply using razor class library.

Regarding the embedded files I would have to take a look. It might have been something blazon specific that we didn’t remove.

Closing as we’ve taken action.

AlseinX commented 5 years ago

@javiercn I am still confused that the razorclasslib template creates a library with .cshtml files while blazor uses .razor files. Does cshtml work also with blazor? And the razorclasslib targets netcoreapp3.0 while the client-side blazor template targets netstandard2.0. How could a blazor project reference a razor class library project?

javiercn commented 5 years ago

There is a flag for it. We simply default now to support pages and views, but the default will change.

minhhieugma commented 5 years ago

Someone can help me how to make the Blazor SSR website work with a component project. The static files in content folder are not copied to wwwroot.

karthickthangasamy commented 5 years ago

@minhhieugma : Rename the content folder with wwwroot and generate the nuget package using dotnet pack.

Reference link - https://devblogs.microsoft.com/aspnet/asp-net-core-and-blazor-updates-in-net-core-3-0-preview-6/

minhhieugma commented 5 years ago

nerate the nuget package using dotnet pack. @karthickthangasamy Thank guy, it works. Can you tell me how to include script/link automatically as blazor client side rendering can do? I would not like to manual add it!

karthickthangasamy commented 5 years ago

@minhhieugma : I hope, for SSR we have to refer it manually like the below. Let's wait for others thoughts about including the assets automatically as like client-side application.

<script src="_content/LIBRARY_NAME/example.js