gpt4 book ai didi

c# - System.NullReferenceException 创建 viewModel

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:02 25 4
gpt4 key购买 nike

因此,我试图找到 Umbraco 节点(作为 iPublishedContent),并将其传递给 viewModel(因为 Ш 劫持了一条路线)。所以我把它放在我的 Controller 中:

private AddCouponCodesViewModel viewModel;
public AddCouponCodesController(){
//Get iPublished content
IPublishedContent content = Umbraco.TypedContent(1225);
//Pass to viewModel
viewModel = new AddCouponCodesViewModel(content);
RouteData.DataTokens["umbraco"] = content;
}
public ActionResult Index()
{
//return view etc
}

但是我得到了

Exception Details: System.NullReferenceException: 
Object reference not set to an instance of an object.

这里:

 Source Error(AddCouponCodesViewModel.cs): 
Line 20:
Line 21: }
Line 22: public AddCouponCodesViewModel(IPublishedContent content)
Line 23: : base(content)
Line 24: {

AddCouponCodeRenderModel.cs:

public class AddCouponCodesViewModel : RenderModel
{
public string test { get; set; }
public List<string> tables { get; set; }
public List<string> errors { get; set; }

public AddCouponCodesViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture)
{

}
public AddCouponCodesViewModel(IPublishedContent content)
: base(content)
{

}

这是 Global.asax

public class Global : UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);

BundleConfig.RegisterBundles(BundleTable.Bundles);
//AreaRegistration.RegisterAllAreas();
//WebApiConfig.Register(GlobalConfiguration.Configuration);
//FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
//RouteConfig.RegisterRoutes(RouteTable.Routes);

base.OnApplicationStarting(sender, e);

RouteTable.Routes.MapRoute(
"AddCouponCodes", // Route name
"Admin/{controller}/{action}/{id}", // URL with parameters
new { controller = "AddCouponCodes", action = "Index", id = "" } // Parameter defaults
);


}

}

内容已发布(我已经核对了两遍),节点ID是正确的。

我在这里基本上想做的是获取路由 example.com/Admin/{controller}/{action}/{parameter}要路由,但在将它与 umbracoNode 连接时遇到问题(类 RenderModel 需要一个 iPublishContent 对象作为参数,但我在尝试传递任何东西时运气不好)

有人可以在这里帮助我吗,我在这上面停留了太多时间:-(

最佳答案

澄清一下,如果您劫持了一条路线,则意味着您正在覆盖 Umbraco 将其 RenderModel 传递到其中一个已发布页面的方式。您可以通过覆盖主要的 RenderMvcController 来全局执行此操作,也可以在特定于 DocumentType 的基础上进行覆盖。因此,例如,如果我有一个主页文档类型,我可以创建:

public HomepageController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
// Create your new renderModel here, inheriting
// from RenderModel

return CurrentTemplate(renderModel);
}
}

这将通过这个操作将所有调用路由到主页。为此,您不需要在路由表中定义任何新路由。并且您应该在操作中覆盖渲染模型,而不是在构造函数中

您的问题有点令人困惑,并且不完全清楚您要实现的目标,因为:

  • 您已经定义了一条路线,并且
  • 在您的构造函数中,您调用 Umbraco.TypedContent(1225) 来检索特定的已发布节点

所以...如果您尝试路由的管理页面本身已由 Umbraco 发布(并且听起来不像),只需创建一个具有页面文档类型名称的新 Controller 并覆盖以上述方式渲染模型。

但是......如果你的管理页面还没有被 Umbraco 发布,而你只是想让管理页面访问节点数据,那么你有几个选择:

  • 创建一个表面 Controller ,继承自 SurfaceController。这将使您能够访问 Umbraco 上下文等;或
  • 创建一个标准 Controller (最好在一个区域中)并使用类似 Autofac 的东西注入(inject) ContentCache

例如:

builder.RegisterControllers(typeof (AdminController).Assembly)
.WithParameter("contentCache", UmbracoContext.Current.ContentCache);
  • 创建一个标准 Controller (最好在区域中)并使用 Umbraco 的 ContentService API 访问节点,即 new Umbraco.Core.Services.ContentService().GetById(1225)

后两种方法的区别在于:

  • 注入(inject) ContentCache 可让您以只读方式快速访问已发布内容。
  • 访问 ContentService 可为您提供对节点本身的读/写访问权限,但会以牺牲速度为代价,因为您直接查询数据库。

这取决于你的要求。

无论哪种方式,都值得花时间通读 documentation for hijacking Umbraco routes ,并至少尝试了解正在发生的事情。

关于c# - System.NullReferenceException 创建 viewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com