简体   繁体   English

如何使用Resx文件管理门户内容

[英]How to use resx files to manage portal content

We are developing an e-commerce system that multiple affiliate partners will use. 我们正在开发一个电子商务系统,多个关联伙伴将使用该系统。 We would like to tailor the portal for each partner and be able to accommodate slight variations in content from page to page. 我们希望为每个合作伙伴量身定制门户,并能够适应页面之间内容的细微变化。 Our current technique has been to create a copy of a .cshtml view for each partner and make the customization to each view. 我们当前的技术是为每个合作伙伴创建一个.cshtml视图的副本,并对每个视图进行自定义。 Our designer is groaning because may of these views only have slight variations in wording. 我们的设计师之以鼻,因为这些观点可能在措辞上仅略有不同。 We only plan to have 10 or so partners (it cannot expand beyond that because of the size of our industry) so a full blown CMS system is overkill. 我们仅计划有10个左右的合作伙伴(由于我们的行业规模,它不能扩展到这个范围之外),因此全面的CMS系统是过大的。

I would like to use resx files manage content strings for each partner the way one would use them to manage content strings for different languages. 我想使用resx文件来管理每个合作伙伴的内容字符串,就像使用它们来管理不同语言的内容字符串一样。 The end result would be the ability to do something like this in a view. 最终结果将是在视图中执行类似操作的能力。

Please contact customer service at @Properties.Resources.PartnerCustomerServiceEmail 请通过@ Properties.Resources.PartnerCustomerServiceEmail与客户服务联系。

at not have to worry about which resource file is used to resolve the string PartnerCustomerServiceEmail. 不必担心使用哪个资源文件来解析字符串PartnerCustomerServiceEmail。

Thank you in advance for your help 预先感谢您的帮助

First idea that comes to my mind is to save resource file's name in question into viewdata (or Session) and use a helper to get the value. 我想到的第一个想法是将有问题的资源文件的名称保存到viewdata(或Session)中,并使用帮助器获取值。

Say you have two partners: Foo Logistics and Bar Solutions. 假设您有两个合作伙伴:Foo Logistics和Bar Solutions。 Have a resource file for each of them: PartnerFoo.resx and PartnerBar.resx. 每个都有一个资源文件:PartnerFoo.resx和PartnerBar.resx。

In your controller, store the resource file you want to use into ViewData as in: 在控制器中,将要使用的资源文件存储到ViewData中,如下所示:

public ActionResult About()
{
     ...
     ViewData["Resource"] = "MyMVCAppNamespace.Resources.PartnerFoo";
     return View();
}

Include the namespace into the string too. 也将名称空间包括在字符串中。

Then code in the helper to retrieve the resource with viewdata. 然后在帮助程序中编码以使用viewdata检索资源。

Helpers/Helper.cs: 助手/ Helper.cs:

namespace MyMVCAppNamespace.MvcHtmlHelpers
{
    public static class HtmlHelpersExtensions
    {
        public static ResourceManager PartnerResource(this HtmlHelper helper)
        {
            // Get resource filename from viewdata
            string res = helper.ViewContext.ViewData["Resource"].ToString();
            // Load the resource from assembly
            ResourceManager resourceManager = new ResourceManager(res, Assembly.GetExecutingAssembly());

            return resourceManager;
        }
    }
}   

Now in the view, we are gonna use this helper to retrieve the string we want to write: 现在,在视图中,我们将使用此帮助程序来检索我们要编写的字符串:

About.cshtml: About.cshtml:

    @Html.PartnerResource().GetString("PartnerName")
    @Html.PartnerResource().GetString("PartnerCustomerServiceEmail")

Which gets rendered as: 呈现为:

Foo Logistics service@foologistics.com

or with PartnerBar 或使用PartnerBar

Bar Solutions service@barsolutions.com

We determine the resource file to use before the view is loaded. 我们确定在加载视图之前要使用的资源文件。 Then in view it gets dynamically rendered according to what resource is stored in to the viewdata. 然后在视图中,它根据存储在视图数据中的资源来动态渲染。 You can even store the resource filename into web.config and load the string in helper from there if you want. 您甚至可以将资源文件名存储到web.config中,并根据需要从那里将字符串加载到帮助器中。

What's even more cool is that if you have localized resx file, say PartnerFoo.fi.resx and then use different culture (fi-FI in this case), the resourcemanager automatically looks up the localized version. 更酷的是,如果您已本地化resx文件(例如PartnerFoo.fi.resx),然后使用其他区域性(在这种情况下为fi-FI),那么资源管理器会自动查找本地化版本。

You can even do simple branding by storing image URLs and whatnot in the resource file. 您甚至可以通过在资源文件中存储图像URL和其他内容来进行简单的品牌宣传。

It's simple really, but I hope it gets you started. 确实很简单,但我希望它能帮助您入门。

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

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