get url of image from Umbraco examine search UDI - lucene

I'm using Umbraco with Lucene and Examine
I'm trying to get the url of a image but at the moment I get the following
"umb://media/57ad107794724d0289b4f9fe44c298a8"
How can I get the URL for the media from the UDI, my code attempt so far is below.
foreach (var item in searchResults)
{
var content = Umbraco.Content(item.Fields["id"]);
if (item.Fields.Keys.Contains("image"))
{
var image = item.Fields["image"].Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
var pathToImage = string.Join(",", image);
var mediaItem = Umbraco.TypedContent(pathToImage);
var test3 = mediaItem.Url; <--------------------Throws NullReferenceException
}
}
Any help appreciated

Try the following
if (item.Fields.Keys.Contains("image"))
{
var imgUdi = item.Fields["image"];
var udi = Udi.Parse(imgUdi);
var mediaTest = Umbraco.TypedMedia(udi);
string pathToImage = mediaTest.Url;
}
Notice how I'm using Udi.Parse to get the ID, then you can use that to get the url

Related

Reading and DeserializeObject Response Header using Asp Core 3.1

I'm try to make a paging for collection, I have a Web API and I'm sending paging data into header like total count and next page link so I can read the response header and put it into string as a Json here is my consume code
public async Task<MultibleValuesHelper> GetAllTags()
{
IEnumerable<TagModelDto> tags;
IEnumerable<PagedDataModel> Header=null;
using (var response = await _httpClient.GetAsync($"tag"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
if( response.Headers.TryGetValues("X-Pagenation", out var Pagenation))
{
var TryHeader = Pagenation.FirstOrDefault();
Header = JsonConvert.DeserializeObject<List<PagedDataModel>>(TryHeader);
}
tags = JsonConvert.DeserializeObject<List<TagModelDto>>(apiResponse);
}
return new MultibleValuesHelper {
TagServiceCollection = tags,
HeaderPagenation = Header
};
}
my problem when I'm try to Deserialize header it showen Error :JsonSerializationException: Cannot deserialize the current JSON object So how I can Deserialize Header and put it into IEnumerable Header so I can handle it
Okay the answer is a little bit change in the code we will use PagedDataModel instead List, and here is the full code
public async Task<MultibleValuesHelper> GetAllTags()
{
IEnumerable<TagModelDto> tags;
PagedDataModel Header=new PagedDataModel ;
using (var response = await _httpClient.GetAsync($"tag"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
if( response.Headers.TryGetValues("X-Pagenation", out var Pagenation))
{
var TryHeader = Pagenation.FirstOrDefault();
Header = JsonConvert.DeserializeObject<PagedDataModel>(TryHeader);
}
tags = JsonConvert.DeserializeObject<List<TagModelDto>>(apiResponse);
}
return new MultibleValuesHelper {
TagServiceCollection = tags,
HeaderPagenation = Header
};
}
this is will work Thanks for all of you

How can I add release description to a TAG using GITLAB API

Using
GET local/api/v4/projects/project_ID/repository/tags/TAG_NAME/
I can get info about TAG. How to ask only about RELEASE DESCRIPTION of this TAG?
How to PUT RELEASE DESCRIPTION to a specific TAG, it used to works for me something like that:
static async Task<string> add_tag_release_notes2(string repository, int appId, string tag_name, string notes_description) //PUT or POST last TAG notes description
{
string req_path = which_repo(repository)[0] + "projects/" + appId + "/repository/tags/" + tag_name + "/release";
using (var request = new HttpRequestMessage(new HttpMethod("POST"), req_path)) //PUT won't work as well
{
request.Headers.TryAddWithoutValidation("PRIVATE-TOKEN", which_repo(repository)[1]);
request.Content = new StringContent(notes_description, System.Text.Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
var add_tag_response = JsonConvert.DeserializeObject<Tag>(content);
string notes = response.StatusCode.ToString();
return notes;
}
but now i get response : 404 not found
even
GET local/api/v4/projects/project_ID/repository/tags/TAG_NAME/release
won't work :(
They moved release enter link description here

.Net-Core get HttpBrowserCapabilities from httpContext

I want to use the HttpBrowserCapabilities in .net-core. I tried it following way:
var userAgent = httpContext.Request.Headers["user-agent"];
var userBrowser = new HttpBrowserCapabilities { Capabilities = new Hashtable { { string.Empty, userAgent } } };
var factory = new BrowserCapabilitiesFactory();
factory.ConfigureBrowserCapabilities(new NameValueCollection(), userBrowser);
var mobileString = userBrowser.IsMobileDevice ? "(mobil)" : string.Empty;
var browserString = $"{userBrowser.Browser} version {userBrowser.Version} {mobileString} OS: {userBrowser.Platform}";
But an InvalidCastException is thrown. How ca i get this code retun the right values?
Got it!
userAgent is of the type Microsoft.Extensions.Primitives.StringValues.
But HttpBrowserCapabilities expects a string as input parameter.
Therefore i had to check if userAgent contains a value (userAgent.Any()) and use the following code
var userBrowser = new HttpBrowserCapabilities {Capabilities = new Hashtable {{string.Empty, userAgent.First()}}};

Web Api Service

I am Consuming web api service from one project (project1) to another project (project2) but when i try to get the response from project2 to project1 with this code
using (var client = new WebClient())
{
string json = URL;
Var jdownload = client.DownloadString(json);
}
It's adding back slashed into "jdownload" Var please guide me how i can get that response without that backslashes
i.e
“servicefor” = “GetData”,
“settings” = {
\"stringname\":5,
\"url\":\"null",
\"services\":\"GetSet\",
\"interval\":2.0,
\"alldatacontains\":200,
\"netamountname\":\"BulkFetch\"
}
I got the solution how can i get the Clean Jason
public JObject GetData(string dataO)
{
var data = MethodToGetData(dataO);
JObject pobj = JObject.FromObject(new
{
type = "stgdats",
val = "val",
d_id = id,
sat = data
}
);
return pobj;
}

WinRT No mapping for the Unicode character exists in the target multi-byte code page

I am trying to read a file in my Windows 8 Store App. Here is a fragment of code I use to achieve this:
if(file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
var size = stream.Size;
using(var inputStream = stream.GetInputStreamAt(0))
{
DataReader dataReader = new DataReader(inputStream);
uint numbytes = await dataReader.LoadAsync((uint)size);
string text = dataReader.ReadString(numbytes);
}
}
However, an exeption is thrown at line:
string text = dataReader.ReadString(numbytes);
Exeption message:
No mapping for the Unicode character exists in the target multi-byte code page.
How do I get by this?
I managed to read file correctly using similar approach to suggested by duDE:
if(file != null)
{
IBuffer buffer = await FileIO.ReadBufferAsync(file);
DataReader reader = DataReader.FromBuffer(buffer);
byte[] fileContent = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(fileContent);
string text = Encoding.UTF8.GetString(fileContent, 0, fileContent.Length);
}
Can somebody please elaborate, why my initial approach didn't work?
Try this instead of string text = dataReader.ReadString(numbytes):
dataReader.ReadBytes(stream);
string text = Convert.ToBase64String(stream);
If, like me, this was the top result when search for the same error regarding UWP, see the below:
The code I had which was throwing the error (no mapping for the unicode character exists..):
var storageFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
using (var stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
using (var dataReader = new DataReader(stream))
{
await dataReader.LoadAsync((uint)stream.Size);
var json = dataReader.ReadString((uint)stream.Size);
return JsonConvert.DeserializeObject<T>(json);
}
}
What I changed it to so that it works correctly
var storageFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
using (var stream = await storageFile.OpenAsync(FileAccessMode.Read))
{
T data = default(T);
using (StreamReader astream = new StreamReader(stream.AsStreamForRead()))
using (JsonTextReader reader = new JsonTextReader(astream))
{
JsonSerializer serializer = new JsonSerializer();
data = (T)serializer.Deserialize(reader, typeof(T));
}
return data;
}