简体   繁体   English

Null Blazor 托管的 ConfigureServices 中的 WebRootPath

[英]Null WebRootPath in ConfigureServices for Blazor Hosted

I would like to access the web root path from within Blazor.Server.Startup.ConfigureServices in a Blazor Hosted solution.我想在 Blazor 托管解决方案中从Blazor.Server.Startup.ConfigureServices中访问 web 根路径。

I have injected IWebHostEnvironment into the Startup constructor and saved it as a property called "WebHostEnvironment".我已将IWebHostEnvironment注入Startup构造函数并将其保存为名为“WebHostEnvironment”的属性。 The problem is that in ConfigureServices, WebHostEnvironment.WebRootPath is always null.问题是在 ConfigureServices 中, WebHostEnvironment.WebRootPath总是 null。

I have already verified that:我已经验证过:

  • wwwroot folder exists in my Blazor.Client project. wwwroot文件夹存在于我的 Blazor.Client 项目中。
  • app.UseStaticFiles(); has been called in Startup.Configure (not sure if this is necessary, but it is one of the few recommended solutions I've found).已在Startup.Configure中调用(不确定是否有必要,但这是我发现的为数不多的推荐解决方案之一)。

This seems to be a common problem (perhaps WebRootPath is only populated under certain conditions - such as after my app is published/deployed?), however I've been unable to find a clear, definitive answer anywhere and I'm not sure what I can do to obtain the web root path in ConfigureServices .这似乎是一个常见问题(也许 WebRootPath 仅在某些情况下才会填充 - 例如在我的应用程序发布/部署之后?),但是我无法在任何地方找到明确,明确的答案而且我不确定是什么我可以在ConfigureServices中获取 web 根路径。

Use Case用例

While not directly relevant to the question, I'm trying to obtain the path to my token endpoint so that that I can configure the TokenUrl property in a new OpenApiAuthFlow for Swashbuckle's AddSwaggerGen service.虽然与问题没有直接关系,但我正在尝试获取令牌端点的路径,以便我可以在新的OpenApiAuthFlow中为 Swashbuckle 的AddSwaggerGen服务配置TokenUrl属性。 I'd prefer not to have to specify the localhost URL since this will eventually be deployed to a production website.我不想指定本地主机 URL,因为它最终会部署到生产网站。 I suppose I could store it in appsettings if needed, but I had hoped not to have to maintain the path there.我想如果需要我可以将它存储在 appsettings 中,但我希望不必在那里维护路径。

services.AddSwaggerGen(options =>
    {
        options.SwaggerDoc("v1", new OpenApiInfo {Title = "Protected API", Version = "v1"});
        options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
        {
            Type = SecuritySchemeType.OAuth2,
            Flows = new OpenApiOAuthFlows
            {
                AuthorizationCode = new OpenApiOAuthFlow
                {
                    AuthorizationUrl = new Uri("https://localhost:5000/connect/authorize"),
                    TokenUrl = new Uri("https://localhost:5000/connect/token"),
                    Scopes = new Dictionary<string, string>
                    {
                        {"api1", "Demo API - full access"}
                    }
                }
            }
        });
    });

It's not a bug but a feature;这不是一个错误,而是一个特性; you just cannot do what you want in ConfigureServices .您只是无法在ConfigureServices中做您想做的事。
If you want to access to the wwwroot folder put your code in Configure method or in a method of ServiceCollection you can get a IServiceProvider instance such as .AddTransient .如果您想访问wwwroot文件夹,请将代码放入Configure方法或ServiceCollection方法中,您可以获得IServiceProvider实例,例如.AddTransient

sample样本

services.AddTransient(p =>
{
    var environment = p.GetRequiredService<IWebHostEnvironment>();
    var path = environment.WebRootPath;
    return new Stuff(path);
});

Try adding a "wwwroot" folder to your Blazor server project (assuming you have the standard 3 projects in your solution - client, server, and shared).尝试将“wwwroot”文件夹添加到您的 Blazor 服务器项目(假设您的解决方案中有标准的 3 个项目 - 客户端、服务器和共享)。 The folder itself can be empty, but it needs to be there, otherwise the WebRootPath will be null.该文件夹本身可以为空,但它必须存在,否则 WebRootPath 将为 null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM