Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC6 razor how to detect debug mode?

In previous versions of Razor I would conditionally load minified/debug versions of scripts by rendering a partial view that looked something like this:

@if (Context.IsDebuggingEnabled)
{
    <script src="~/debug.js"></script>
}
else
{
    <script src="~/release.js"></script>
}

If MVC6, vNext, VS2015, or whatever you call it :) I don't know how to accomplish this. Anyone know how?

like image 608
CHS Avatar asked Nov 29 '14 10:11

CHS


People also ask

Can you debug Razor pages?

Set a breakpoint on line 17. Run the app again. You can even debug the Razor Pages with markup and inspect the values of variables!

How do I debug Aspnet?

Select the ASP.NET Core project in Visual Studio Solution Explorer and click the Properties icon, or press Alt+Enter, or right-click and choose Properties. Select the Debug tab and click the link to open the Open debug launch profiles UI.

How do I enable Razor runtime compilation?

Enable runtime compilation at project creationIn the Create a new ASP.NET Core web application dialog: Select either the Web Application or the Web Application (Model-View-Controller) project template. Select the Enable Razor runtime compilation checkbox.


1 Answers

In MVC6, you can use the environment tag helper to load different versions of scripts depending for Development vs Production environments. This is based on the value of the value of the ASPNET_ENV environment variable.

<environment names="Development">            
    <script src="~/debug.js"></script>
</environment>
<environment names="Staging,Production">
    <script src="~/release.js"></script>
</environment>

Bundling and minification would be handled using a task like Gulp or Grunt.

I have outlined the new approach in detail here: http://www.davepaquette.com/archive/2015/05/05/web-optimization-development-and-production-in-asp-net-mvc6.aspx

like image 147
David Paquette Avatar answered Nov 07 '22 05:11

David Paquette