I would like to embed svg directly into my ASP.net-MVC view so that the output looks something like this:
<html>
<body>
<svg> ... </svg>
</body>
</html>
With PHP I would do this like so:
<? include('image.svg'); ?>
I tried @Html.Partial('image.svg')
but got an error. Is there a way to fix this or another method I can try?
To embed an SVG via an <img> element, you just need to reference it in the src attribute as you'd expect. You will need a height or a width attribute (or both if your SVG has no inherent aspect ratio).
You can embed SVG elements directly into your HTML pages.
Can we cache inline SVG? Why yes, yes we can. css-tricks.com/inline-svg-cac… It's nice to read, but in a real-world app, it would be an over-engineering.
Renaming SVG files and using Html.Partial should do the trick, but I didn't like to be renaming files in order to achieve it. So, this way, you can keep your files as they are.
@Html.Raw(File.ReadAllText(Server.MapPath("~/image.svg")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With