Initial page
Steps to add React to an existing ASP.NET MVC Project
Last updated
Was this helpful?
Steps to add React to an existing ASP.NET MVC Project
Last updated
Was this helpful?
Based off
All packages and dependencies that are required for our React project are tracked in package.json. Therefore, before starting off, we need to create package.json file in the application root folder, using the following npm command.
Once package.json has been created, include this file in your ASP.NET solution. Open the file and fill in any blanks to prevent warning errors when installing subsequent packages. You may also need to add in the repository property as below:
Now install react and react-dom followed by the rest of the below packages
The first npm command will create the node_modules folder and also install the latest react and react-dom package.
Apart from the react packages we have also installed webpack, typescript and ts-loader.
The typescript package lets us write our code in typescript which is lot easier and cleaner. However, browsers don't understand typescript and therefore we need a package like ts-loader to transpile our typescript into javascript.
With all the javascript that is generated across so many files and packages, there is a lot of code that needs to be downloaded which can impact performance over the web. In order to optimise performance we then use a tool like Webpack to bundle all our js code in to one minified file.
For Webpack to run with ASP.NET MVC we need to setup webpack.config.js. If you don't see it in in the Add | New Item menu, you will need to install the VS extension webpack task runner.
Once you add webpack.config.js to your project, make the following updates to the project too:
Decide the source folder for your React files. You can stick to the default naming convention and create a folder called src or choose any other name. Include this folder in your project.
Create index.tsx in this folder. This will be the entry point in to our app.
Update webpack.config.js by changing the "entry" property to the above file
Also change the "output" property to "./reactApp.js".
We will also be setting the mode to development for now and watch : true in our config. This will keep track of all the changes made to the .js files and bundle them automatically
Remove the devserver section and add source map settings devtool: ‘source-map’. This will keep track of source maps while bundling our .js files and also help with debugging code with our browser devTools.
In the module section, remove the existing "loaders" property which uses babel and change to ts-loader and exclude "node_modules'"
The final webpack.config.js will look as below:
For us to start using typescript, we also need to add tsconfig.json file in our project. Once added, add the below options to it.
Next add the "tsc" and "start" entries to the scripts section in package.json
This config setting will allow us to compile our code using either nodejs or webpack task runner. The start command will now build our typescript files and then webpack will bundle it. Although we can use npm commands to compile our typescript code, for now but we will do it through the Visual Studio task runner.
In Visual Studio, select View | Other Windows | Task Runner Explorer | Run - Run Development. If everything has been configured properly so far, you should see the below output in Task Runner Explorer.
Once the above is done, you will notice a new "dist" folder with the file reactAppp.js with in it.
We can now add code to our MVC View files and index.tsx and check out the output.
But before we begin, we need to add a scripts tag to our Layout file in ASP.NET MVC. Add this tag as far below as possible to avoid javascript errors. If you have multiple Layout files, you can add it in the topmost root Layout file.
In the view file you wish to add the React component, add the following line. In the demo, the file is Index.cshtml
In Index.tsx add the below.
Users familiar with React will recognise the above. The React scripts will find the HTML element with the Id "app" and replace this with our React component - in this case the H1 Hello React tag.
Run and done!