Configuring React with Asp.net MVC4,getting error like Failed to load resource[app.jsx] in browser

Viewed 2156

Index.cshtml

@{
  ViewBag.Title = "Home Page";
 }

<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="~/Scripts/react/react.min.js"></script>
<script src="@Url.Content("~/Scripts/app/app.jsx")"></script>

app.jsx

var HelloWorld = React.createClass({
    render: function () {
        return (
            <div>Hello World!!</div>
        );
}
});

getting error in browser like

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

4 Answers

Resolved this issue with following steps,

1) Make a sure installed React.Web.Mvc4 Package, it is named in NuGet manager as ReactJS.NET

2) Install the following packages from NuGet manager,

 JavaScriptEngineSwitcher.V8
 JavaScriptEngineSwitcher.V8.Native.win-x64
 JavaScriptEngineSwitcher.V8.Native.win-x86 

3) In App_Start directory update a file ReactConfig.cs

    public static void Configure()
    {
        JsEngineSwitcher.Current.DefaultEngineName = V8JsEngine.EngineName;
        JsEngineSwitcher.Current.EngineFactories.AddV8();
    }

4) Clean, Build the solution.

First, your code seems to be ok.

You can look at the url that give you error 500 on the browser. localhost/.../.jsx

you may have something like this

Error

If you looks at https://reactjs.net/getting-started/aspnet.html

Install those NuGet

On your project you must have reactconfig.cs

reactconfig.cs file

you need to configurare as in the picture

reactconfig

that's it.

Try to transpile your .jsx files to .js with babel or web compiler (install from VS extensions) and reference those from the view. Also, remove the second react lib:

<script src="~/Scripts/react/react.min.js"></script>

Resolved by adding this to ConfigureServices in Startup.cs:

services.AddJsEngineSwitcher(options => 
     {
           options.DefaultEngineName = V8JsEngine.EngineName;
           options.EngineFactories.AddV8();
     }
);
Related