because it says it's "inaccessible due to its protection level". Is there a way to get around this or fix it? Or is there an alternative code I could use to accomplish the same thing?
I'm sorry, I'm entirely self-taught and I miss a lot of the basics - could you dumb this down a little bit further? Where do I actually put the helper function code? Is this something that can be called across the site in views, partials, etc.?
It looks like what I need though, so thank you for helping me along!
Helper is just a C# class, you can put it anywhere in the Visual Studio solution:
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Umbraco.Cms.Core.Models.PublishedContent;
public static class SvgHelper
{
public static async Task<HtmlString> RenderInlineSvgFromUrlAsync(this IHtmlHelper helper, string relativeUrl, string cssClass, Dictionary<string, string> additionalAttributes = null)
{
// ...
}
}
I'm probably missing something, but it looks like this can't be used in my Partial files because of the async. Trying to find a fix just leads to different errors and I have a feeling the site's just not structured the way it should be to allow me to use this.
I do appreciate you trying to help me out, though!
Yeah, that's what throws the error: "Can only be used within an async method". The Partials aren't async (not that I fully understand what that means).
It looks like it would work within a View, but most of the site is made up of Partials as widgets or specific pieces of code.
No worries, it took me a while to figure it out so I feel your pain. That is such a handy helper function to have as well, I'm glad I was able to help.
It looks like the line var svgContent = await httpClient.GetStringAsync(absoluteUrl); is throwing an error in the log and causing a 404 error when the site is Published. I'm not familiar enough with what's going on with the code to figure out what could be going wrong. If you have any ideas of where I should look for how to fix it, I'd really appreciate it.
Outputting SVG code from Media Picker for customization
I found this page on customizing SVGs and it's exactly what I need, but I'm unable to use "HostingEnvironment" in the following code:
because it says it's "inaccessible due to its protection level". Is there a way to get around this or fix it? Or is there an alternative code I could use to accomplish the same thing?
I use the following helper function to get me the content of SVGs:
I can then call it like this:
I'm sorry, I'm entirely self-taught and I miss a lot of the basics - could you dumb this down a little bit further? Where do I actually put the helper function code? Is this something that can be called across the site in views, partials, etc.?
It looks like what I need though, so thank you for helping me along!
Helper is just a C# class, you can put it anywhere in the Visual Studio solution:
Here's Microsoft's documentation on HtmlHelpers:
https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs
Thank you!
I'm probably missing something, but it looks like this can't be used in my Partial files because of the async. Trying to find a fix just leads to different errors and I have a feeling the site's just not structured the way it should be to allow me to use this.
I do appreciate you trying to help me out, though!
Did you call it using await?
Yeah, that's what throws the error: "Can only be used within an async method". The Partials aren't async (not that I fully understand what that means).
It looks like it would work within a View, but most of the site is made up of Partials as widgets or specific pieces of code.
Try render your partial like this:
And the MS docs:
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-8.0
Okay, I had to restructure some of the code within the Partial I was working in, then I had to change this bit:
because it was throwing a System.UriFormatException error, to:
I'm not sure if it's just because I'm working on localhost right now and I'll need to add that back in, but it's working!
I can't thank you enough for being patient and helping me figure this out.
No worries, it took me a while to figure it out so I feel your pain. That is such a handy helper function to have as well, I'm glad I was able to help.
Hi Brendan,
It looks like the line
var svgContent = await httpClient.GetStringAsync(absoluteUrl);
is throwing an error in the log and causing a 404 error when the site is Published. I'm not familiar enough with what's going on with the code to figure out what could be going wrong. If you have any ideas of where I should look for how to fix it, I'd really appreciate it.is working on a reply...