Ludohen Ludohen - 6 months ago 42
ASP.NET (C#) Question

ASP.NET .cshtml razor file transform on Build

I'm trying to find a way to transform my index.cshtml file when building my project.
For instance make this:

<script src="app-dev.js"></script>

become this when Build mode is

<script src="app-prod.js></script>


Ok, this will work.

Add an appsetting for it:

<add key="Environment" value="dev"/>

Then in your view add this:

<script src="app-@(System.Web.Configuration.WebConfigurationManager.AppSettings["Environment"]).js></script>

In your other environments simply use transforms to replace it i.e.

    <add key="Environment" value="prod" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />

An alternative and I feel a better approach would be to abstract out the WebConfiguration manager as an interface and have the app setting set in a model instead.

If it is in a common layout and set everytime maybe create a base model and have it set in an OnActionExecuted in a base controller instead.