In this article, I'm starting from scratch to create a new ASP.NET MVC application with ReactJS.Net and I'm also adding a React component to it. I`m using Visual Studio to create the ASP.NET MVC application and will install NuGet package ReactJS.NET with ES6. I am creating a simple "Hello World" application.
Let’s start to create the ASP.NET MVC application,
- Open Visual Studio from the Start menu or taskbar icon
- After that click on - File => New => Project
- Open a "New Project" popup and select Template => Visual C# => Web => ASP.NET Web Application (.NET Framework)
- Open popup (dialog) - New ASP.NET Application – FirstReactApp
- Now, we see ASP.NET MVC application folder structure in the solution as per the below screenshot.
- Install ReactJS.NET (React.Web.Mvc4) NuGet Package from Manage NuGet Packages
- => Open a dialog box and click on OK
- => again open another dialog popup for licenses acceptance and click on "I Accept" button
- And also install "Web.Optimization.React" from "Manage NuGet Packages" using the same steps as above.
- Now, add a controller by right-clicking on the controller folder from the solution.
- using System.Web.Mvc;
- namespace FirstReactApp.Controllers {
- public class HomeController: Controller {
- // GET: Home
- public ActionResult Index() {
- return View();
- }
- }
- }
- Add the required CDN files on schtml viewReact Component by Creating .JSX
- @{
- ViewBag.Title = "Index";
- }
- <div id="myContainer"></div>
- <!--All Required REACTJS Lib-->
- <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="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.6.0/prop-types.js"></script>
- Now, we need to create a first component to create a file (.jsx) by right clicking on container folder script => react folder select a file from new items dialog popup and click on Add button. fThe file is called : "FirstReactApp.jsx"
React is all about modular components. For the Hello World example, we'll have the following component structure as per below. Now I`m going to create a react component called "HelloWorld" inside the simple div => h1 => text.
I am using ES6 to create components by using the class keyword.
What is ES6?
ECMAScript 6 (ES6, often referred to as “Harmony”) is the upcoming sixth major release of the ECMAScript language specification. ECMAScript is the “proper” name for the language commonly referred to as JavaScript. I won’t go into the history of these names, but if you’re interested there’s plenty of information around the web.
(source:https://dev.venntro.com/2013/09/es6-part-1/)
Using ES6 with React version v0.13 introduces a new way to create a component by defining a class component that is a new "Class" keyword and module syntax.
Use the new modern ES6 pattern to create "HelloWorld" component likes below:
Scripts => react => FirstReactApp.jsx
- class HelloWorld extends React.Component {
- render() {
- return (
- < div >
- < h1 > Hello World React JS! < /h1>
- < /div>)
- }
- }
- ReactDOM.render( < HelloWorld / > , document.getElementById('myContainer'));
- NET Bundling and Minification
- using System.Web;
- using System.Web.Optimization;
- using System.Web.Optimization.React; //Add this namespace
- namespace FirstReactApp {
- public class BundleConfig {
- public static void RegisterBundles(BundleCollection bundles) {
- bundles.Add(new BabelBundle("~/bundles/main").Include("~/Scripts/react/FirstReactApp.jsx"));
- }
- }
- }
- Add the main bundle as mentioned above into _Layout.cshtml page
- @Scripts.Render("~/bundles/main")
- Run the Project to display the expected output screen
No comments:
Post a Comment