Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Episerver CMS中,如何创建属性以显示上传图像或PDF资产的源URL(在Episerver编辑器界面中)?_C#_Asp.net Mvc_Asp.net Mvc 5_Episerver_Episerver 10 - Fatal编程技术网

C# 在Episerver CMS中,如何创建属性以显示上传图像或PDF资产的源URL(在Episerver编辑器界面中)?

C# 在Episerver CMS中,如何创建属性以显示上传图像或PDF资产的源URL(在Episerver编辑器界面中)?,c#,asp.net-mvc,asp.net-mvc-5,episerver,episerver-10,C#,Asp.net Mvc,Asp.net Mvc 5,Episerver,Episerver 10,令人烦恼的是,Episerver中的MediaData类没有任何基本属性,如源URL、alt text等。我正在尝试实现一个类来继承MediaData并为特定类型的媒体资产(本例中为PDF)提供特定属性 我尝试过手动设置属性值,并重写SetDefaultValues事件,但没有成功。尽管如此,我确实看到了基于“SrcUrl”类型的文本框或URL选择器,但是它是空的,并且从不填充上传的PDF的URL [ContentType( DisplayName = "PDF File", G

令人烦恼的是,Episerver中的MediaData类没有任何基本属性,如源URL、alt text等。我正在尝试实现一个类来继承MediaData并为特定类型的媒体资产(本例中为PDF)提供特定属性

我尝试过手动设置属性值,并重写SetDefaultValues事件,但没有成功。尽管如此,我确实看到了基于“SrcUrl”类型的文本框或URL选择器,但是它是空的,并且从不填充上传的PDF的URL

[ContentType(
    DisplayName = "PDF File",
    GUID = "xxxxxxx-xxxx-xxxxxx-xxxx-xxxxxxxxxxx")]
[MediaDescriptor(ExtensionString = "pdf")]
public class PdfFile : MediaData
{
    [UIHint(UIHint.MediaFile)]
    [Display(Name = "PDF URL",
        Description = "Link to view or reference PDF",
        GroupName = SystemTabNames.Content,
        Order = 10)]
    public virtual string SrcUrl
    {
        get { return UrlResolver.Current.GetUrl(this.ContentLink); }
        set { value = UrlResolver.Current.GetUrl(this.ContentLink); }
    }

    // Sets the default property values
    public override void SetDefaultValues(ContentType contentType)
    {
        base.SetDefaultValues(contentType); 

        this.SrcUrl = UrlResolver.Current.GetUrl(this.ContentLink) ?? "Default";
    }
}


****免责声明:我是Episerver CMS的新手,可能遗漏了一些非常简单的东西(如果合适的话,可以被羞辱)。*

我不确定您需要完成什么,但可能需要注意的是,在编辑
MediaData
内容时,可以通过单击发布按钮获得其URL(请注意下载此文件的链接):


编辑:另一个建议是创建自定义编辑器dijit(使用Dojo)对于
MediaFile
UI提示。这将允许您显示URL,而无需添加其他属性。

在Epi支持的帮助下,我能够解决这一问题,基本上我需要创建一个可初始化的模块-然后允许我连接到内容创建所需的生命周期事件

[InitializableModule]
[ModuleDependency(typeof(InitializationModule))]
public class ModelDefaultValuesInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        contentEvents.CreatedContent += MediaBlocksDefaultValues;
    }

    private void MediaBlocksDefaultValues(object sender, ContentEventArgs e)
    {
        PopulateAssetURL(e);
    }

    /// <summary>
    /// Get the URL path of the uploaded asset and set it to the SrcUrl field which is easily visible to editors
    /// </summary>
    /// <param name="e"></param>
    private void PopulateAssetURL(ContentEventArgs e)
    {
        var mediaTypeBlock = e.Content as PdfFile;
        if (mediaTypeBlock != null)
        {
            string result = ServiceLocator.Current.GetInstance<UrlResolver>().GetUrl(mediaTypeBlock.ContentLink);
            if (!string.IsNullOrEmpty(result))
            {
                var srvcLoc = ServiceLocator.Current.GetInstance<IContentRepository>();
                var contentClone = mediaTypeBlock.CreateWritableClone() as PdfFile;
                contentClone.SrcUrl = result;
                srvcLoc.Save(contentClone, SaveAction.Publish, EPiServer.Security.AccessLevel.Administer);
            }
        }            
    }

    public void Uninitialize(InitializationEngine context)
    {
        var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        contentEvents.CreatedContent -= MediaBlocksDefaultValues;
    }
}
[初始化模块]
[模块依赖性(类型(初始化模块))]
公共类ModelDefaultValuesInitialization:IIInitializableModule
{
公共void初始化(InitializationEngine上下文)
{
var contentEvents=ServiceLocator.Current.GetInstance();
contentEvents.CreatedContent+=MediaBlocksDefaultValue;
}
私有void MediaBlocksDefaultValue(对象发送方、ContentEventArgs e)
{
PopulateAssetURL(e);
}
/// 
///获取上传资产的URL路径,并将其设置为编辑器容易看到的SrcUrl字段
/// 
/// 
私有void PopulateAssetURL(ContentEventArgs e)
{
var mediaTypeBlock=e.内容为Pdfile;
if(mediaTypeBlock!=null)
{
字符串结果=ServiceLocator.Current.GetInstance().GetUrl(mediaTypeBlock.ContentLink);
如果(!string.IsNullOrEmpty(结果))
{
var srvcLoc=ServiceLocator.Current.GetInstance();
var contentClone=mediaTypeBlock.CreateWritableClone()作为Pdfile;
contentClone.SrcUrl=结果;
srvcLoc.Save(contentClone、SaveAction.Publish、epserver.Security.AccessLevel.administrate);
}
}            
}
公共void取消初始化(InitializationEngine上下文)
{
var contentEvents=ServiceLocator.Current.GetInstance();
contentEvents.CreatedContent-=MediaBlocksDefaultValue;
}
}
  • 注意:我发布上述内容是为了保持一致,但实际上我对代码进行了重构,以便在名为BaseMediaData的类上设置SrcUrl属性,该类继承自MediaData类。通过这种方式,所有文件类型(powerpoints、PDF等)都可以具有此属性,因为它与所有文件类型都相关。图像继承自不同的基类(ImageData),因此我还必须创建一个名为ImageFile的新类,该类继承自ImageData并应用相同的属性。我很懒,但使用接口来标准化这两个类的实现可能是个好主意

不确定从何处获得
.SrcUrl
,查看了他们的
epserver.Core.MediaData
及其继承的类和接口实现,我没有看到
SrcUrl
@12seconds,SrcUrl是我试图添加到类中的新属性,而不是它的现有属性(因此它不会出现在文档中)。在该文档中,有一个内容GUID,可以以迂回的方式使用它来获取指向内容的链接…但是基类中没有任何内容提供对内容所在URL的实际引用。非常感谢,我不知道链接隐藏在其中。我基本上希望呈现完全相同的链接,但作为一种重新呈现我们的编辑更容易看到和访问的纯广告属性。我有一张Epi支持的票证,因此,一旦我有了解决方案,我一定会更新这里的问题。如果我最终使用了您的解决方案之一,我一定会将此标记为正确答案。谢谢Ted!