创建一个正则表达式以从 umbraco 中的节点获取图像值
Create a regex to grab image value from node in umbraco
我正在尝试使用
从 umbraco 中的节点获取图像
var image = node.GetProperty("postImage").Value;
但是我得到
"{\r\n \"focalPoint\": {\r\n \"left\": 0.5,\r\n \"top\": 0.5\r\n },\r\n \"src\": \"/media/8354/Image123Test.jpg\",\r\n \"crops\": [\r\n {\r\n \"alias\": \"blogPost\",\r\n \"width\": 200,\r\n \"height\": 200\r\n },\r\n {\r\n \"alias\": \"thumbnail\",\r\n \"width\": 50,\r\n \"height\": 50\r\n },\r\n {\r\n \"alias\": \"featuredImage\",\r\n \"width\": 320,\r\n \"height\": 238\r\n }\r\n ]\r\n}"
returned.
是否有可能提出 return "/media/8354/Image123Test.jpg\" 的正则表达式?
谢谢,
你可以尝试这样的事情。它搜索 \"src\": \"
并获取下一个 \"
之前的所有内容
string test = "{\r\n \"focalPoint\": {\r\n \"left\": 0.5,\r\n \"top\": 0.5\r\n },\r\n \"src\": \"/media/8354/Image123Test.jpg\",\r\n \"crops\": [\r\n {\r\n \"alias\": \"blogPost\",\r\n \"width\": 200,\r\n \"height\": 200\r\n },\r\n {\r\n \"alias\": \"thumbnail\",\r\n \"width\": 50,\r\n \"height\": 50\r\n },\r\n {\r\n \"alias\": \"featuredImage\",\r\n \"width\": 320,\r\n \"height\": 238\r\n }\r\n ]\r\n}";
string regx = "(?<=\\"src\\": \\").+(?=\")";
System.Text.RegularExpressions.Match mat = System.Text.RegularExpressions.Regex.Match(test, regx);
它看起来像 json。因此,您不能将此字符串解析为 Json 并从键 "src" 中检索值。例如,您可以使用 Newtonsoft.Json
using Newtonsoft.Json.Linq;
...
string json = node.GetProperty("postImage").Value;
var parsedJson = JObject.Parse(json);
var srcValue = parsedJson.Properties().FirstOrDefault(item => item.Name == "src")?.Value;
您似乎在使用 Umbraco 中的图像裁剪器数据类型。你不需要自己解析 JSON,Umbraco 已经为此内置了实用程序。
尝试(在 Umbraco 7.3.5+ 中):
// Where "node" is IPublishedContent.
// "blogpost" is the alias of your crop setting.
// "Url" is of type System.Web.Mvc.UrlHelper
IHtmlString imageUrl = Url.GetCropUrl(node, "postImage", "blogpost");
这将按照您在 Umbraco 中为裁剪设置别名指定的宽度、高度和居中获取图像。
.GetCropUrl
方法中还有更多重载,可让您在代码中指定宽度和高度。
查看文档 here。
根据媒体项目,umbracoFile 可能存储为 JSON 或仅存储路径。出于这个原因,我创建了这两个小方法来确保我始终获得文件路径。
private static string GetServerFilePath(IMedia mediaItem, bool isNew)
{
string filePath = (string)mediaItem.Properties["umbracoFile"].Value;
if (!isNew || filePath.Contains("{"))
{
filePath = GetExistingFilePath(filePath);
}
return HttpContext.Current.Server.MapPath(filePath);
}
private static string GetExistingFilePath(string filePath)
{
var jsonFileDetails = JObject.Parse(filePath);
string src = jsonFileDetails["src"].ToString();
filePath = src;
return filePath;
}
我正在尝试使用
从 umbraco 中的节点获取图像var image = node.GetProperty("postImage").Value;
但是我得到
"{\r\n \"focalPoint\": {\r\n \"left\": 0.5,\r\n \"top\": 0.5\r\n },\r\n \"src\": \"/media/8354/Image123Test.jpg\",\r\n \"crops\": [\r\n {\r\n \"alias\": \"blogPost\",\r\n \"width\": 200,\r\n \"height\": 200\r\n },\r\n {\r\n \"alias\": \"thumbnail\",\r\n \"width\": 50,\r\n \"height\": 50\r\n },\r\n {\r\n \"alias\": \"featuredImage\",\r\n \"width\": 320,\r\n \"height\": 238\r\n }\r\n ]\r\n}"
returned.
是否有可能提出 return "/media/8354/Image123Test.jpg\" 的正则表达式?
谢谢,
你可以尝试这样的事情。它搜索 \"src\": \"
并获取下一个 \"
string test = "{\r\n \"focalPoint\": {\r\n \"left\": 0.5,\r\n \"top\": 0.5\r\n },\r\n \"src\": \"/media/8354/Image123Test.jpg\",\r\n \"crops\": [\r\n {\r\n \"alias\": \"blogPost\",\r\n \"width\": 200,\r\n \"height\": 200\r\n },\r\n {\r\n \"alias\": \"thumbnail\",\r\n \"width\": 50,\r\n \"height\": 50\r\n },\r\n {\r\n \"alias\": \"featuredImage\",\r\n \"width\": 320,\r\n \"height\": 238\r\n }\r\n ]\r\n}";
string regx = "(?<=\\"src\\": \\").+(?=\")";
System.Text.RegularExpressions.Match mat = System.Text.RegularExpressions.Regex.Match(test, regx);
它看起来像 json。因此,您不能将此字符串解析为 Json 并从键 "src" 中检索值。例如,您可以使用 Newtonsoft.Json
using Newtonsoft.Json.Linq;
...
string json = node.GetProperty("postImage").Value;
var parsedJson = JObject.Parse(json);
var srcValue = parsedJson.Properties().FirstOrDefault(item => item.Name == "src")?.Value;
您似乎在使用 Umbraco 中的图像裁剪器数据类型。你不需要自己解析 JSON,Umbraco 已经为此内置了实用程序。
尝试(在 Umbraco 7.3.5+ 中):
// Where "node" is IPublishedContent.
// "blogpost" is the alias of your crop setting.
// "Url" is of type System.Web.Mvc.UrlHelper
IHtmlString imageUrl = Url.GetCropUrl(node, "postImage", "blogpost");
这将按照您在 Umbraco 中为裁剪设置别名指定的宽度、高度和居中获取图像。
.GetCropUrl
方法中还有更多重载,可让您在代码中指定宽度和高度。
查看文档 here。
根据媒体项目,umbracoFile 可能存储为 JSON 或仅存储路径。出于这个原因,我创建了这两个小方法来确保我始终获得文件路径。
private static string GetServerFilePath(IMedia mediaItem, bool isNew)
{
string filePath = (string)mediaItem.Properties["umbracoFile"].Value;
if (!isNew || filePath.Contains("{"))
{
filePath = GetExistingFilePath(filePath);
}
return HttpContext.Current.Server.MapPath(filePath);
}
private static string GetExistingFilePath(string filePath)
{
var jsonFileDetails = JObject.Parse(filePath);
string src = jsonFileDetails["src"].ToString();
filePath = src;
return filePath;
}