我正在使用MVC ASP.NET Core 3.1和React.NET,并且遇到了这个问题。
当我渲染组件时,组件将渲染,但是prop始终为null。几乎就像Html.React渲染方法没有正确传递值一样,请帮忙!
我只会在反应中添加relevent代码(我的startup.cs具有更多设置)
启动文件
public void ConfigureServices(IServiceCollection services) {
services.AddJsEngineSwitcher(options => options.DefaultEngineName = ChakraCoreJsEngine.EngineName).AddChakraCore();
services.AddReact();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseReact(config =>
{
// If you want to use server-side rendering of React components,
// add all the necessary JavaScript files here. This includes
// your components as well as all of their dependencies.
// See http://reactjs.net/ for more information. Example:
config
.AddScript("~/scripts/react_common/login.jsx");
config.SetLoadBabel(true);
});
}
index.cshtml(或任何视图,仅尝试使用此HTML扩展帮助器)
@Html.React("Login", new
{
Test = "Test"
}, serverOnly: true)
login.jsx
class Login extends React.Component {
render() {
return <div>{this.props.Test}</div>
}
不管我做什么,它都永远不会显示“测试”。我需要知道为什么它没有将值传递给道具。我开始对此问题失去理智,在开始迁移到.NET Core之前,它工作得很好。
更多详细信息(Nuget软件包)
React.Asp.Net(5.1.2)React.AspNet.Middleware(5.1.2)
请帮忙。
默认的JSON序列化程序合同解析器设置为自动将其转换为camelCase(反应)。如果希望它保持提供的大小写,则必须重写此行为-在startup.cs中的Configure方法中:
app.UseReact(...
app.UseStaticFiles();
//Ensure to place this after the UseRact statement above
ReactSiteConfiguration.Configuration.JsonSerializerSettings.ContractResolver = new DefaultContractResolver();
希望这可以帮助其他人免于发疯
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句