关于上下文的上一个问题(上一个问题无处解决,所以我创建了这个新的问题以重新开始):Unable to Create New MVC Widget in Kentico 12
我正在尝试创建一个名为“带有摘要的图像”的小部件。现在,我只是想向其中添加一个属性(一个具有RTF编辑器的摘要属性)。
当我向页面构建器添加新的小部件时,它没有显示为小部件选项:
由此,您可以看到我已正确配置了页面构建器(已经添加了“富文本”小部件),可以看到可以添加小部件(“富文本”小部件来自NuGet软件包,我安装了名为“ Kentico.EMS12.MvcComponents.Widget.RichText”),您会看到我只有两个可用的小部件(“表单”和“富文本”),这两个小部件都不是我要添加的小部件
我需要您的帮助来弄清楚为什么我的新窗口小部件没有出现在此对话框中。
这是Visual Studio中的解决方案资源管理器,显示了我为此新小部件创建的所有文件:
这是我的属性类的样子:
// ImageWithSummaryProperties.cs
namespace RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
{
using Kentico.PageBuilder.Web.Mvc;
public class ImageWithSummaryProperties : IWidgetProperties
{
public string Summary { get; set; }
}
}
这是我的视图模型的样子:
// ImageWithSummaryViewModel.cs
namespace RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
{
public class ImageWithSummaryViewModel
{
public string Summary { get; set; }
}
}
这是我的控制器的外观:
// ImageWithSummaryController.cs
using System.Web.Mvc;
using Kentico.PageBuilder.Web.Mvc;
using RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary;
[assembly: RegisterWidget(
identifier: "Rhythm.ImageWithSummary",
controllerType: typeof(ImageWithSummaryController),
name: "Image with Summary",
Description = "An image with summary text.",
IconClass = "icon-l-img-3-cols-3")]
namespace RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
{
public class ImageWithSummaryController : WidgetController<ImageWithSummaryProperties>
{
public ActionResult Index()
{
var properties = GetProperties();
return PartialView("Widgets/_Rhythm.ImageWithSummary", new ImageWithSummaryViewModel()
{
Summary = properties.Summary
});
}
}
}
这是我的看法:
@* _Rhythm.ImageWithSummary.cshtml *@
@using Kentico.PageBuilder.Web.Mvc
@using RhythmAgency.KenticoWebsite.Widgets.ImageWithSummary
@using Kentico.Components.Web.Mvc.InlineEditors
@model ImageWithSummaryViewModel
@if (Context.Kentico().PageBuilder().EditMode)
{
Html.Kentico().RichTextEditor(nameof(ImageWithSummaryProperties.Summary), null);
}
else
{
<div class="fr-view">
@Html.Raw(Html.Kentico().ResolveRichText(Model.Summary))
</div>
}
我不会过多地关注视图文件,因为我什至无法将小部件添加到页面构建器中,因此视图甚至没有机会被调用。
这是我的主视图文件:
@* Views/Home/Index.cshtml *@
@using Kentico.PageBuilder.Web.Mvc
@using Kentico.Web.Mvc
<h1>Rhythm Agency</h1>
@Html.Kentico().EditableArea("main")
我真的很茫然,为什么这个小部件没有出现在要添加到页面部分的可用小部件列表中。这是一些额外的上下文:
EditableArea
时,您可以看到我对可使用的小部件没有任何限制。编辑:
有人好奇为什么这个问题和上一个问题不同,因此本次编辑澄清了这一点。上一个问题是有关我尝试仅使用属性类实现的小部件的。这个较新的问题使用另一种方法(即使用控制器),这是在Kentico中实现小部件的另一种方法。
编辑#2:
有人建议我将小部件注册程序集属性放在App_Start
文件夹中,但是这样做没有帮助:
所以为什么仍然无法解决仍然是个谜。
答案 0 :(得分:1)
要识别控制器和小部件,您需要将控制器放置在“ / Controllers”文件夹中。我的小部件控制器位于“ / Controllers / Widgets”文件夹中。
我遇到的问题包括没有在类名中添加后缀“ Controller”,以及小部件控制器不在“ / Controllers”文件夹中的问题。
您还不是在单独的项目中工作?因为这需要您在'AssemblyInfo.cs'中使用以下内容
using CMS;
[assembly: AssemblyDiscoverable]
并确保已在kentico项目中启用了页面构建器功能。例如:
protected void Application_Start()
{
...
// Gets the ApplicationBuilder instance
// Allows you to enable and configure Kentico MVC features
ApplicationBuilder builder = ApplicationBuilder.Current;
// Enables the preview feature
builder.UsePreview();
// Enables the page builder feature
builder.UsePageBuilder();
...
}