Skip to content
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

Razor Class Library - Static File Path #14562

Closed
nfplee opened this issue Sep 29, 2019 · 12 comments
Closed

Razor Class Library - Static File Path #14562

nfplee opened this issue Sep 29, 2019 · 12 comments
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate

Comments

@nfplee
Copy link

nfplee commented Sep 29, 2019

I'm trying to remove the Startup class in my application. Therefore I have the changed the CreateHostBuilder method in my Program class to the following:

public static IHostBuilder CreateHostBuilder(string[] args) {
    return Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => {
            webBuilder.UseApplication(Assembly.GetExecutingAssembly().FullName);
        });
}

Where UseApplication looks like:

public static IWebHostBuilder UseApplication(this IWebHostBuilder hostBuilder, string entryAssemblyName) {
    return hostBuilder.ConfigureServices((context, services) => ...)
        .Configure(app => ...)
        .UseSetting(WebHostDefaults.ApplicationKey, entryAssemblyName);
}

Note: Without the UseSetting(WebHostDefaults.ApplicationKey, entryAssemblyName) line the application doesn't run at all.

However I am having issues where the path to my static files in my razor class libraries (e.g. "~/_content/MyLibrary/js/app.js") now returns a 404.

I found that if I call the Configure extension method in my Program.cs file instead of the UseApplication extension method then it works fine so I tried looking into the source to see if I can fix it but so far everything I have tried has not worked.

I'm not sure if this is a bug or if there is some way to configure it that I have not found. I'd really appreciate the help.

Thanks

@rynowak
Copy link
Member

rynowak commented Sep 30, 2019

Usually that happens when you're missing a call to .UseContentRoot(Directory.GetCurrentDirectory()). This tells the server to use the current directory as the root directory for the app.

I'm not sure that's you're problem though since you're using CreateDefaultBuilder.

@Tratcher
Copy link
Member

I found that if I call the Configure extension method in my Program.cs file instead of the UseApplication extension method then it works fine so I tried looking into the source to see if I can fix it but so far everything I have tried has not worked.

Is UseApplication defined in a different assembly?

.UseSetting(WebHostDefaults.ApplicationKey, entryAssemblyName); would normally deal with this.

@nfplee
Copy link
Author

nfplee commented Sep 30, 2019

@rynowak thanks but it didn't make a difference.

@Tratcher yeah UseApplication is in another assembly.

@nfplee
Copy link
Author

nfplee commented Sep 30, 2019

I've attached a simple sample application which shows the problem.

WebApplication1.zip

@analogrelay
Copy link
Contributor

However I am having issues where the path to my static files in my razor class libraries (e.g. "~/_content/MyLibrary/js/app.js") now returns a 404.

This seems like an issue in @javiercn 's static file manifest stuff.

@analogrelay analogrelay added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed area-hosting labels Oct 3, 2019
@analogrelay
Copy link
Contributor

Bouncing over to MVC because it seems to be an issue in the static files support in Razor Class Libraries and I don't have a ton of context there.

@soeleman
Copy link

soeleman commented Oct 6, 2019

With current sample WebApplication1.zip, without any modification.

The static file 404 only happen when we running on IISExpress.
14562_IISExpress

The Project work fine
14562_Project

And also fine on dotnet run
14562_DotnetRun

@javiercn
Copy link
Member

javiercn commented Oct 7, 2019

@nfplee Seems you are not using generic host.

There might be an issue lurking with the old hosting mode. I'd recommend you move to use generic web host and that should fix it, if not, alterntively try calling UseStaticWebAssets on your IWebHostBuilder and that should also fix it.

@nfplee
Copy link
Author

nfplee commented Oct 7, 2019

@javiercn I am using the generic host. See my original post and my sample. I've also tried calling UseStaticWebAssets but it did not make a difference.

@javiercn
Copy link
Member

javiercn commented Oct 7, 2019

@nfplee I downloaded your app and it doesn't seem to be using generic host.

In any case, I switched it to use generic host and found that the issue is that you are passing in the full name (which contains version, etc) and that causes issues when static web assets tries to find the manifest file. If you switch it to use the assembly simple name, everything works:

Assembly.GetExecutingAssembly().GetName().Name

Alternatively, you can pass in the path to the static web assets manifest by providing the path in the setting WebHostDefaults.StaticWebAssetsKey

So to summarize it:

  • Move to generic web host.
  • Switch to using Assembly.GetExecutingAssembly().GetName().Name

@javiercn
Copy link
Member

javiercn commented Oct 7, 2019

Closing this issue as there's no more action for us to take here.

@javiercn javiercn closed this as completed Oct 7, 2019
@nfplee
Copy link
Author

nfplee commented Oct 8, 2019

@javiercn - Thanks, it looks like I over simplified the example as I thought I was using the generic web host. I used the generic web host in the original question. Your fix also seems to have done the trick.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate
Projects
None yet
Development

No branches or pull requests

7 participants