Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding SVG in ASP.net page

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?

like image 543
flyingfisch Avatar asked Apr 06 '15 20:04

flyingfisch


People also ask

How do I embed an SVG file?

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).

Can you embed SVG in HTML directly?

You can embed SVG elements directly into your HTML pages.

Can SVG be cached?

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.


1 Answers

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")))
like image 176
Alejandro Rizzo Avatar answered Nov 07 '22 07:11

Alejandro Rizzo