Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# 来自PropertyUrl的SimpleAddress 我有一个特定的页面,既有长的Page1/Page2/MyPage地址,也有简单的MyPage地址。_C#_Episerver - Fatal编程技术网

C# 来自PropertyUrl的SimpleAddress 我有一个特定的页面,既有长的Page1/Page2/MyPage地址,也有简单的MyPage地址。

C# 来自PropertyUrl的SimpleAddress 我有一个特定的页面,既有长的Page1/Page2/MyPage地址,也有简单的MyPage地址。,c#,episerver,C#,Episerver,然后我想通过PropertyUrl在某个地方引用它: [CultureSpecific] [Required] [BackingType(typeof(PropertyUrl))] [Display( Name = "Link", Description = "Link to the page", GroupName = SystemTabNames.Content, Order = 1)] public virtual Url Link { get; set;

然后我想通过PropertyUrl在某个地方引用它:

[CultureSpecific]
[Required]
[BackingType(typeof(PropertyUrl))]
[Display(
    Name = "Link",
    Description = "Link to the page",
    GroupName = SystemTabNames.Content,
    Order = 1)]
public virtual Url Link { get; set; }
我希望简单地址(如果存在)用于路由或url呈现,而不是长地址

我正在寻找一些优雅的解决方案,如果它存在的话

对其进行了一些修改,以更好地适应我的任务:

public static string GetExternalUrl(this Url url)
{
    var content = UrlResolver.Service.Route(new UrlBuilder(url));

    return GetExternalUrl(content);
}

public static string GetExternalUrl(this ContentReference contentReference)
{
    if (ContentReference.IsNullOrEmpty(contentReference)) return null;

    var content = ServiceLocator.Current.GetInstance<IContentLoader>().Get<IContent>(contentReference);

    return GetExternalUrl(content);
}

public static string GetExternalUrl(this IContent content)
{
    var externalProperty = content?.Property["PageExternalURL"];

    return !string.IsNullOrWhiteSpace(externalProperty?.ToString()) ? $"/{externalProperty.ToString().Trim('/')}/" : null;
}