Last week I started a new web project to test out ASP.NET Core and Visual Studio 2019. With all of the changes between the IDE, project configuration, and MVC boilerplate code I figured waiting until work and life slowed down a bit would be ideal. With the end of the year near the slowdown has finally happened.
The boilerplate code setup hasn’t been difficult, and as expected, well documented. At this point in the project I’ve setup all references to data access classes outside of the main project to utilize dependency injection. .NET Core makes this extremely simple I and recommend all developers who are hesitant to use DI to take a look at Microsoft’s documentation.
Two areas that stumped me for a bit were areas that were relatively simple in Visual Studio 2017. The first one was getting TypeScript to compile and build the mappings properly for debugging. Prior to .NET Core a tsconfig.json file in the project’s Scripts directory was all that was needed. However, ASP.NET MVC projects in .NET Core have a different setup. The guidance is to create a scripts directory outside of the wwwroot directory in the project to hold the TypeScript files. In order to get the JavaScript code that is generated from those files to be used in the deployed site you then need to copy the JavaScript, TypeScript, and Map files over to your JavaScript directory within wwwroot. From what I have read, to do this you need to perform a few steps. They are pretty basic and I encourage you to read the article on TypeScriptLang.org as it has more details on the steps needed. What follows are the two files I had to play with to get the mappings working.
The first file to add to your project is tsconfig.json. You can place the file at the root of your project or in the main folder holding your TypeScript files. For the purposes of this post I’ll be putting the file in the root of my project.
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true, // Generate the source Map file connecting JS to TS
"target": "es5",
"typeRoots": [ // Point to NPM for typings
"node_modules/@types"
]
},
"exclude": [
"node_modules", // Folder to ignore
"wwwroot" // Folder to ignore
],
"include": [
"scripts" // The folder to look for TypeScript files
]
}
Once this configuration file is in place any build launched will fire off the TypeScript compiler and generate the JavaScript and Map files.
The next piece of the puzzle is getting the generated files into the correct directory in the wwwroot folder of the project. To accomplish this another file needs to be added to the project in order to use gulp. The gulpfile.js will have a simple job, delete the old JavaScript directory and copy over the TypeScript, JavaScript, and Map files to the wwwroot directory.
gulpfile.js
var gulp = require('gulp');
var del = require('del');
var paths = {
scripts: ['scripts/app/**/*.js', 'scripts/app/**/*.ts', 'scripts/app/**/*.map'],
};
gulp.task('clean', function () {
return del(['wwwroot/js/**/*.js', 'wwwroot/js/**/*.map', 'wwwroot/js/**/*.ts']);
});
gulp.task('default', function () {
gulp.src(paths.scripts).pipe(gulp.dest('wwwroot/js'))
});
Once those files were in place the next battle was getting my project to rebuild and run any updates made while debugging the site. I originally thought the problem resided with the IDE configuration. Turns out this isn’t the case and what was needed were changes at the Project level and in the code.
The steps are all spelled out by Microsoft in Razor file compilation in ASP.NET Core. To help with the code piece of the update take a look below. In the Startup.cs file the constructor and ConfigureServices sections of code will need to be updated in a similar fashion.
Startup.cs
private IWebHostEnvironment _env { get; set; }
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
_env = env;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
IMvcBuilder builder = services.AddRazorPages();
#if DEBUG
if (_env.IsDevelopment())
{
builder.AddRazorRuntimeCompilation();
}
#endif
...
}
Once these changes were finally realized my project was back in a productive development state. Visual Studio 2019 does have TypeScript compilation settings that can be set in the project Properties that could be used instead of the tsconfig.json file. And it might be possible to not use the gulp task by having your TypeScript files within the wwwroot/js folder. I’ve shied away from this setup for the time being since multiple articles have emphasized keeping the TypeScript code outside of wwwroot and using the tsconfig.json file makes the project more automated build friendly.