4

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)

Nawrez
  • 3,314
  • 8
  • 28
  • 42

4 Answers4

4

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.

2

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.

Gustavo
  • 51
  • 5
  • I only configured as follows: JsEngineSwitcher.Current.DefaultEngineName = V8JsEngine.EngineName; JsEngineSwitcher.Current.EngineFactories.AddV8(); I did not add the .AddScript("~/Scripts/bla/bla") and the MVC Razor view finds and renders the jsx file. – Armando Servin Mar 03 '21 at 02:01
0

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>
Martin Shishkov
  • 2,709
  • 5
  • 27
  • 44
0

Resolved by adding this to ConfigureServices in Startup.cs:

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