我正在尝试设置一个 ASP.Net MV5 应用程序以与ReactJS.Net一起使用,包括服务器端渲染和捆绑。
不幸的是,它因以下异常而失败:
React.dll 中出现“React.TinyIoC.TinyIoCResolutionException”类型的异常,但未在用户代码中处理
附加信息:无法解析类型:React.ReactEnvironment
此异常发生在此行:
@Scripts.Render("~/bundles/ui-components")
这条线是我的_layouts.cshtml
文件。
我应该如何解决我的问题?
为了详细说明,这里我做了什么:
在 BundleConfig.cs 中:
bundles.Add(new ScriptBundle("~/bundles/reactjs").Include( "~/Scripts/react/react-{version}.js")); bundles.Add(new JsxBundle("~/bundles/ui-components") .Include("~/content/ui/components/*.jsx"));
我用我所有的 jsx 文件创建了一个文件夹“Content/ui/components”(实际上只有一个来自教程的“commentsbox.jsx”文件。
在 ReactConfig.cs 中,我删除了该
WebActivatorEx.PreApplicationStartMethod
属性,因为 MVC5 不再支持该属性,这得益于 Owin 组件。它仍然包含:public static class ReactConfig { public static void Configure() { ReactSiteConfiguration.Configuration .AddScript("~/content/ui/*.jsx"); } }
在我的
Global.asax.cs
文件中,我显式调用ReactConfig.Configure
方法来替换WebActivatorEx.PreApplicationStartMethod
钩子:public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { ReactConfig.Configure(); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } }
我的
_layouts.cshtml
视图包含(在文件的底部):@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @Scripts.Render("~/bundles/reactjs") @Scripts.Render("~/bundles/ui-components") @RenderSection("scripts", required: false) @Html.ReactInitJavaScript()
在我看来:
@{ ViewBag.Title = "Home Page"; } <div id="content"></div> @section scripts { @Html.React("CommentBox", new {}, containerId:"content") }
最后,我的 jsx 文件:
var CommentBox = React.createClass({ render: function() { return ( <div class="row"> <div class="col-md-4"> hello </div> </div> ); } });