Full-Stack MVC Application with Redux
This post covers the end-to-end process (wiring MVC and React) of creating a brand new ASP.NET MVC website and adding a React component in it. We will start from scratch and end with a fully functioning component.
We can use .NET Framework, but you can instead use .NET Core if you want to be able to run your site on Linux or Mac OS. Currently .NET Core is missing some of the functionality provided by .NET Framework, so it is recommended to use .NET Framework unless you have a reason to use .NET Core specifically (eg. cross-platform support).
Start by creating a new ASP.NET Core MVC project:
- File → New → Project
- Ensure “.NET Framework 4.6” is selected in the dropdown list at the top
- Go to Templates → Visual C# → Web and select the “ASP.NET Core Web Application (.NET Framework)” template.
We need to install ReactJS.NET to the newly-created project. This is accomplished using NuGet, a package manager for .NET. Right-click on your project in the Solution Explorer and select “Manage NuGet Packages”. Click the “Browse” tab, search for “React.AspNet”, and install the React.AspNet package.
We also need to modify the Startup.cs
file to initialize ReactJS.NET. Open Startup.cs
and perform the following changes:
At the top of the file, add:
<code class="csharp language-csharp" data-lang="csharp"><span class="k">using</span> <span class="nn">Microsoft.AspNetCore.Http</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">React.AspNet</span><span class="p">;</span>
</code>
Directly above:
<code class="csharp language-csharp" data-lang="csharp"><span class="c1">// Add framework services.</span>
<span class="n">services</span><span class="p">.</span><span class="n">AddMvc</span><span class="p">();</span>
</code>
Add:
<code class="csharp language-csharp" data-lang="csharp"><span class="n">services</span><span class="p">.</span><span class="n">AddSingleton</span><span class="p"><</span><span class="n">IHttpContextAccessor</span><span class="p">,</span> <span class="n">HttpContextAccessor</span><span class="p">>();</span>
<span class="n">services</span><span class="p">.</span><span class="n">AddReact</span><span class="p">();</span>
</code>
Directly above:
<code class="csharp language-csharp" data-lang="csharp"><span class="n">app</span><span class="p">.</span><span class="n">UseStaticFiles</span><span class="p">();</span>
</code>
Add:
<code class="csharp language-csharp" data-lang="csharp"><span class="c1">// Initialise ReactJS.NET. Must be before static files.</span>
<span class="n">app</span><span class="p">.</span><span class="n">UseReact</span><span class="p">(</span><span class="n">config</span> <span class="p">=></span>
<span class="p">{</span>
<span class="c1">// If you want to use server-side rendering of React components,</span>
<span class="c1">// add all the necessary JavaScript files here. This includes</span>
<span class="c1">// your components as well as all of their dependencies.</span>
<span class="c1">// See http://reactjs.net/ for more information. Example:</span>
<span class="c1">//config</span>
<span class="c1">// .AddScript("~/Scripts/First.jsx")</span>
<span class="c1">// .AddScript("~/Scripts/Second.jsx");</span>
<span class="c1">// If you use an external build too (for example, Babel, Webpack,</span>
<span class="c1">// Browserify or Gulp), you can improve performance by disabling</span>
<span class="c1">// ReactJS.NET's version of Babel and loading the pre-transpiled</span>
<span class="c1">// scripts. Example:</span>
<span class="c1">//config</span>
<span class="c1">// .SetLoadBabel(false)</span>
<span class="c1">// .AddScriptWithoutTransform("~/Scripts/bundle.server.js");</span>
<span class="p">});</span>
</code>
Finally, add this to Views\_ViewImports.cshtml
:
<code class="csharp language-csharp" data-lang="csharp"><span class="n">@using</span> <span class="n">React</span><span class="p">.</span><span class="n">AspNet</span></code>