简体   繁体   中英

How do I create a hyperlink on a Razor page to an image stored in wwwroot?

How do I create a hyperlink to an image stored in wwwroot on a Razor page?

Example:

@{
  var WFR = "~/images/Certifications/" + (@Model.WFR ??  "../LogoSmall.jpg");
  <img src="@WFR" asp-append-version="true" />  // Works great!
  <a asp-action="@WFR">see document</a>    // Fails miserably
}

The img tag renders the image just fine. But the anchor tag prepends a controller/action which routes me to my error page. This is what the tag helper generates: https://localhost:44394/Employment/~/images/Certifications/12144abb-6af2-424f-b005-4c2039228934_WFR2016.jpg. Setting asp-controller="" just brings me to the home controller.

What am I missing?

You don't need any tag helpers for this, just set the tag to link to your image -

<a href="@WFR" target="_blank">see document</a>

A user in another forum suggest this: Url.Content()

  <a href="@Url.Content(@WFR)">see document</a>

... and it works!!

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