Public Conditional Razor Views with minified JS an

2019-05-06 16:48发布

问题:

I am using ASP.NET MVC 5 to develop my website. I'm using Grunt to minify my CSS and JS. Everything works fine, but I need to change the route source of my files if I'm making a publish (release mode) or I'm debugging (I need to read my CSS and JS clearly).

Something like this:

[Debug Mode]

<script src="scripts/myscript.js"></script>

[Relase Mode or Public Mode]

<script src="dist/myscript.min.js"></script>

I have read this post on StackOverflow(Razor view engine, how to enter preprocessor(#if debug)) and I don't like the proposed solution because I think (not sure) that the loaded Razon View is going to check always in Production Server if it's in release or debug mode and I think that it's not necessary.

Can someone confirm if I am right? Will I will need make the changes manually? Any other solution?

Thanks!! Regards!!

回答1:

One way you could accomplish this is to just check for debug mode in the http context.

@if (HttpContext.Current.IsDebuggingEnabled)
{
    <script src="scripts/myscript.js"></script>
}
else
{
    <script src="dist/myscript.min.js"></script>
}