-3

我有一些特定于每个剃须刀视图的数据,并且我不想将其硬编码到每个视图。所以,我想将视图相关的编译时数据添加到每个视图中。

  • 自定义属性对我不起作用,因为我们无法将自定义属性添加到剃刀视图。
  • 我不想为每个请求或到达视图时从数据源(字典等)重新获取/填充此数据。

那么,有没有办法在 asp.net 应用程序的整个生命周期中一次将数据附加到每个视图?

注意 实际上我想为每个视图静态添加 webpack 生成的脚本/样式。它们的链接包含哈希值,因此当源脚本/样式更改时它们会更改。因此,我只想通过 asp.net 应用程序将它们添加到每个视图一次(相当于将它们输入视图),而不是每次加载视图时。

4

3 回答 3

1

I created a demo application for you here.

You will want to use your appsettings.json file, and inject your settings into your view.

In my appsettings.json I added a section called "ViewConfiguration":

"ViewConfiguration": {
    "ExampleKey": "ExampleValue"
}

Your various values will need to go into your ViewConfiguration section.

For example where I have ExampleKey, you will use a generic name like "IndexPageStyleSheet", and where I have ExampleValue, you will need to update each release with the new stylesheet path. This will only need to be updated when the filename changes.

I then created a ViewConfiguration class which stores all of the values from the appsettings.json file.

You will need to create one property per configuration line, and ensure that the name of the property matches the name of the key in your appsettings.json.

For example where my appsettings.json has ExampleKey, my ViewConfiguration class also has an ExampleKey.

public class ViewConfiguration {
    public string ExampleKey { get; set; }
}

In your Startup.cs you will need to tell your IOC container to load your configuration values into your configuration object.

In my Startup.cs, my ConfigureServices method loads my "ExampleValue" into ViewConfiguration.ExampleKey automatically.

    public void ConfigureServices(IServiceCollection services) {
        // This line is the magic that loads the values from appsettings.json into a ViewConfiguration object.
        services.Configure<ViewConfiguration>(Configuration.GetSection("ViewConfiguration"));

        services.AddMvc();
    }

Now, in my _ViewImports.cshtml I inject my ViewConfiguration object so that I don't need to inject it into every single page. This can be anywhere in the _ViewImports.cshtml file. If you only want to inject specific configuration per folder, you can create a new _ViewImports.cshtml file per folder and inject different configuration objects into each one. It's flexible.

@using Microsoft.Extensions.Options;

@* Please rename this variable to something more appropriate to your application: *@
@inject IOptions<ViewConfiguration> InjectedViewConfig

Now, in any page, you can simply reference the property in your ViewConfiguration object.

For example in my Index.cshtml, I reference the ViewConfiguration.ExampleKey property by referencing the strongly typed property on InjectedViewConfig.Value, and it outputs "ExampleValue" on the page.

This value could just as easily be injected into a script or css link tag as the name of a file. It's very flexible.

<h1>Value: @InjectedViewConfig.Value.ExampleKey</h1>

Example output

With further research, you will be able to inject these values from any configuration source, such as Azure application settings or Azure Key Vault. Please see this article for more details.

于 2018-01-26T12:16:33.420 回答
0

如果您使用的是 mvc,您可以创建模型并将其添加到视图中。由于您不想为每个视图重新创建,您可以创建只读变量。

static readonly MyModel ModelData = new MyModel { PropName = "Hello" };
public IActionResult Index () => View(ModelData);

在您看来,您现在可以强烈键入该值。如果您正在寻找使用 MVVM,您可以参考ViewModel 概念是否仍然存在于 ASP.NET MVC Core 中?

于 2018-01-20T17:11:20.587 回答
0

实现 IFileProvider 和 IFileInfo 可以在编译时更改视图的内容。因此,我们可以使用模板引擎(即http://dotliquidmarkup.org/)替换并提供视图中的静态数据。

检查这个; https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location

于 2018-01-31T14:15:37.670 回答