简体   繁体   中英

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. 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.

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. The end result would be the ability to do something like this in a view.

Please contact customer service at @Properties.Resources.PartnerCustomerServiceEmail

at not have to worry about which resource file is used to resolve the string 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.

Say you have two partners: Foo Logistics and Bar Solutions. Have a resource file for each of them: PartnerFoo.resx and PartnerBar.resx.

In your controller, store the resource file you want to use into ViewData as in:

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.

Helpers/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:

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

Which gets rendered as:

Foo Logistics service@foologistics.com

or with 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.

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.

You can even do simple branding by storing image URLs and whatnot in the resource file.

It's simple really, but I hope it gets you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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