Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Release:

<script src="app-prod.js></script>
like image 983
Ludohen Avatar asked Oct 20 '22 14:10

Ludohen


1 Answers

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.

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

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.

like image 164
hutchonoid Avatar answered Oct 29 '22 00:10

hutchonoid