简体   繁体   中英

Use cshtml in image tag?

I have an svg code inside a cshtml file. Was wondering if there was a way to add that code/svg into an image tag (so I can take advantage of the image tag's alt tag for accessibility purposes)

circle.cshtml

<svg height="100" width="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
  Sorry, your browser does not support inline SVG.  
</svg> 

myview.cshtml

<img src="@Html.Partial(~/circle.cshtml)" alt="red circle />

The above code doesn't work.

You should create an action in controller and returns this partial view result and use the image src attribute value in view.

in controller:

public ActionResult Circle()
{
    return PartialView("_circle");
}

in razor:

<img src="@Url.Action("Circle")" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM