Asp.net mvc 5 MVC5上的ReactJS.Net无法解析依赖关系
我正在尝试设置一个ASP.NETMV5应用程序,包括服务器端渲染和绑定 不幸的是,它失败了,出现了以下异常: React.dll中出现“React.TinyIoC.TinyIoCResolutionException”类型的异常,但未在用户代码中处理 其他信息:无法解析类型:React.ReactEnvironment 此异常发生在此行:Asp.net mvc 5 MVC5上的ReactJS.Net无法解析依赖关系,asp.net-mvc-5,inversion-of-control,reactjs,tinyioc,reactjs.net,Asp.net Mvc 5,Inversion Of Control,Reactjs,Tinyioc,Reactjs.net,我正在尝试设置一个ASP.NETMV5应用程序,包括服务器端渲染和绑定 不幸的是,它失败了,出现了以下异常: React.dll中出现“React.TinyIoC.TinyIoCResolutionException”类型的异常,但未在用户代码中处理 其他信息:无法解析类型:React.ReactEnvironment 此异常发生在此行: @Scripts.Render("~/bundles/ui-components") 这一行取自我的\u layouts.cshtml文件 我应该如何解决
@Scripts.Render("~/bundles/ui-components")
这一行取自我的\u layouts.cshtml
文件
我应该如何解决我的问题?
以下是我所做的详细说明:
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”文件)WebActivatorEx.PreApplicationStartMethod
属性,因为我发现,在Owin组件的利润中,MVC5不再支持该属性。它仍然包含:
public static class ReactConfig
{
public static void Configure()
{
ReactSiteConfiguration.Configuration
.AddScript("~/content/ui/*.jsx");
}
}
Global.asax.cs
文件中,我显式调用ReactConfig.Configure
方法来替换webactivatex.PreApplicationStartMethod
hook:
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);
}
}
\u layouts.cshtml
视图包含(在文件底部):
@{
ViewBag.Title = "Home Page";
}
<div id="content"></div>
@section scripts
{
@Html.React("CommentBox", new {}, containerId:"content")
}
@{
ViewBag.Title=“主页”;
}
@节脚本
{
@React(“CommentBox”,new{},containerId:“content”)
}
var CommentBox = React.createClass({
render: function() {
return (
<div class="row">
<div class="col-md-4">
hello
</div>
</div>
);
}
});
var CommentBox=React.createClass({
render:function(){
返回(
你好
);
}
});
我终于找到了问题的根源 实际上,错误消息有误导性。在
ReactConfig.Configure
方法中,通配符不起作用(即*.jsx
不起作用)
我将此方法替换为:
public static void Configure()
{
ReactSiteConfiguration.Configuration
.AddScript("~/content/ui/commentsbox.jsx").
.AddScript("~/content/ui/comment.jsx");
}
它解决了这个问题。好的一点,错误消息可能更具描述性。我认为更好的消息隐藏在某个InnerException中。Github存在允许通配符的问题:
public static void Configure()
{
ReactSiteConfiguration.Configuration
.AddScript("~/content/ui/commentsbox.jsx").
.AddScript("~/content/ui/comment.jsx");
}