如何在ASP.NET MVC中使用React

时间:2014-04-02 08:43:40

标签: asp.net-mvc-4 reactjs

我想试用React并开始关注教程here 使用Internet模板,在_Layout.cshtml中添加了所需的脚本引用,并将以下脚本放在About.cshtml中。

此代码与' @' char惹麻烦:

<script type="text/jsx">
  /**
   * @jsx React.DOM
   */
  // The above declaration must remain intact at the top of the script.
  // Your code here
</script>

MVC说:

  

名称&#39; jsx&#39;在当前上下文中不存在

请提供一些指示

问候。

2 个答案:

答案 0 :(得分:10)

我刚刚发布了ReactJS.NET,可让您轻松地将JSX编译为JavaScript。使用ReactJS.NET,您可以将代码放入.jsx文件(例如/Scripts/HelloWorld.jsx),然后通过脚本标记引用它:

<script src="@Url.Content("~/Scripts/HelloWorld.jsx")"></script>

此外,还支持 ASP.NET捆绑和缩小 Cassette


原始答案(2014年4月4日前):

你需要通过写两次来逃避@

<script type="text/jsx">
  /**
   * @@jsx React.DOM
   */
  // The above declaration must remain intact at the top of the script.
  // Your code here
</script>

我目前正在处理一些事情,以便更容易地使用来自ASP.NET的React,这应该很快就会推出。这包括JSX的服务器端编译(动态和通过ASP.NET缩小和组合)和React组件的可选服务器端呈现。请继续关注!

答案 1 :(得分:0)

看一下我的项目,一个在.NET中使用React的入门模板。

https://github.com/pauldotknopf/react-dot-net

该项目解决了其他项目(NodeServices和ReactJS.NET)中存在的许多问题,包括具有匹配服务器/客户端路由的真正同构应用程序。