Introduction
React is a very popular front-end library developed by Facebook. It’s basically used for handling View layer for web and mobile apps. The biggest advantage of React JS is that it allows us to create reusable UI components. There are many popular JavaScript libraries in the market but React is the the most popular, and it has strong and large foundation and community behind it.
When we develop applications in MVC, we use JavaScript, jQuery, and HTML with it. Now, NuGet package team has provided in-built library package for Visual Studio for MVC 4, 5 and ASP.NET Core.
So now, you can directly download the NuGet Package for React.js for your MVC application from npm, and use it in your application easily.
Let’s start with a demo.
- Create New Project -> File >> New >> Project.
- Select “NET Framework 4.6.2” and Templates >> Visual C# >> Web >> ASP.NET Web Application. Call it ReactDemoSharp.
- Now, in the “New ASP.NET Web Application” dialog, select Empty Template. I always recommend you use this template for new sites, as others include large amounts of unnecessary packages that may not even work for you.
- Now, install React JS to newly created .NET project.
- Right click on your project in the Solution Explorer.
- Select “Manage Nuget Packages”.
- Search For “ReactJS.NET" and install the Web.Mvc4)package.
- Go to Solution Explorer, right click on the Controller folder, and click Add >> Controller.
- Name the Controller as “HomeController” and select “Empty MVC Controller” as your template.
- Now, once the Controller has been created, right-click on Return View() and click “AddView”.
- In the AddView Dialog, fill the following details.
- View name: Index
- Create a strongly-typed View: Unticked
- Create as a partial view: Unticked
- Use a layout or master page: Unticked
- Now, replace the content of new view file with the below code.
- @{
- Layout = null;
- }
- <html>
- <head>
- <title>Hello React</title>
- </head>
- <body>
- <div id="content"></div>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/remarkable/1.7.1/remarkable.min.js"></script>
- <script src="@Url.Content("~/Scripts/Tutorial.jsx")"></script>
- </body>
- </html>
- Now, we need to create the JavaScript file (Tutorial.jsx). Right click on your project and select Add >> New Folder, enter “Scripts” as folder name.
- Right click on Folder and select Add >> New Item.
- Select Web >> JSX File, and enter “Tutorial.jsx” as file name. Then, click “Add”.
- React is all about modular, composable components, and basically it has a component structure.
- Now, we will build CommentBox Component , which is just a simple <div>.
- Add the below code to Tutorial.jsx.
- var CommentBox = React.createClass({
- render: function() {
- return (
- <div className="commentBox">
- Hello, world! This is viral jain.
- </div>
- );
- }
- });
- ReactDOM.render(
- <CommentBox />,
- document.getElementById('content')
- );
- Run your application.
You can see “Hello, world! This is viral jain." in your browser.
Summary
In this article, we learned how to use React JS in ASP NET MVC. I hope you like it.