Oliver
Oliver

Reputation: 11597

deployment-specific resources in asp.net MVC?

I have created a simple e-commerce web application. When I made the application, I thought that it would be deployed as a single website, so the .cshtml files look like this:

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

Now the web application will be deployed as a second website. Some of the content needs to change:

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

The two deployments will share the same code, but some of the text content in the .cshtml files will be different.

What is a good method to achieve this?

I have seen the .resx system, but it seems a little heavy. Each deployment would have only one resource file. Also, I expect external programmers/admins to make changes to the resources.

Is .resx suitable?

Upvotes: 2

Views: 130

Answers (2)

Simon Belanger
Simon Belanger

Reputation: 14870

There are a few ways to achieve this. You have explored the resx options (that would work). That content could also be pulled from a database of some sort. Since you are using asp.net, why not look at the Web.config and transforms option.

In your baseline Web.config:

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

If you have a project configuration of SecondCompany (as well as the usual debug and release), you can have a Web.SecondCompany.config as:

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

Upon deployment, you can configure your Web.config to be transformed based on the deployed configuration. Deployment of the SecondCompany configuration will result in:

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

Which can be modified directly without deployment as the Web.config is a simple XML file.

In your view, you can replace the content part with:

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

More information on Web.config transformation available here.

Upvotes: 2

Moo-Juice
Moo-Juice

Reputation: 38825

It sounds like you could do worse than have a database at the back-end (which you probably already have if this is an Online Store), in which you could keep a table of translations which could then be modified on a per-site basis.

_id     _expression    _translation
-----------------------------------
1       msgtitle        Welcome to My Company online store

Which could of course be modified in the 2nd website.

To use translations, load them in to a dictionary of some sort during application initialisation, and use the expression as the key and the translation as the value:

<head>
    <title>Second Company Storefront</title>
    <meta name="description" content="@MyCache.Translate["msgtitle"]" />

</head>

Upvotes: 2

Related Questions