gpt4 book ai didi

c# - asp.net MVC 中特定于部署的资源?

转载 作者:行者123 更新时间:2023-11-30 20:56:12 25 4
gpt4 key购买 nike

我创建了一个简单的电子商务 Web 应用程序。当我制作应用程序时,我认为它会被部署为一个单一的网站,所以 .cshtml 文件看起来像这样:

<head>
<title>My Company Storefront</title>
<meta name="description" content="Welcome to My Company online store" />
....
</head>

现在 Web 应用程序将部署为第二个网站。部分内容需要更改:

<head>
<title>Second Company Storefront</title>
<meta name="description" content="Welcome to Second Company online store" />
....
</head>

这两个部署将共享相同的代码,但 .cshtml 文件中的某些文本内容会有所不同。

什么是实现这一目标的好方法?

我看过.resx系统,不过好像有点重。每个部署将只有一个资源文件。此外,我希望外部程序员/管理员对资源进行更改。

.resx 合适吗?

最佳答案

有几种方法可以实现这一点。您已经探索了 resx 选项(可行)。该内容也可以从某种数据库中提取。由于您使用的是 asp.net,为什么不查看 Web.config 和转换选项。

在您的基线 Web.config 中:

<appSettings>
<add key="Description" value="Welcome to My Company online store"/>
</appSettings>

如果你有一个 SecondCompany 的项目配置(以及通常的调试和发布),你可以有一个 Web.SecondCompany.config 作为:

<?xml version="1.0" encoding="utf-8"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Description" value="Welcome to Second Company online store"
xdt:Locator="Match(key)" xdt:Transform="SetAttributes"/>
</appSettings>
</configuration>

部署后,您可以将 Web.config 配置为根据部署的配置进行转换。 SecondCompany 配置的部署将导致:

<appSettings>
<add key="Description" value="Welcome to Second Company online store"/>
</appSettings>

Web.config 是一个简单的 XML 文件,无需部署即可直接修改。

在您看来,您可以将内容部分替换为:

<head>
<title>My Company Storefront</title>
<meta name="description"
content="@System.Configuration.ConfigurationManager.AppSettings["Description"]" />
....
</head>

提供有关 Web.config 转换的更多信息 here .

关于c# - asp.net MVC 中特定于部署的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com