如何在ASP.NET MVC 4中使用Razor视图展示URL路径下的图片?

2026-04-30 16:3413阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计297个文字,预计阅读时间需要2分钟。

如何在ASP.NET MVC 4中使用Razor视图展示URL路径下的图片?

javapublic class Player { public String ImagePath { get { return ~/Content/img/sql_error.JPG; } } 这是我的.cs文件: @model SoulMasters.Models.Game.Player @{ ViewBag.Title=Game; Layout=~/Views/Shared/_Layout.cs; }

我有以下型号:

public class Player { public String ImagePath { get { return "~/Content/img/sql_error.JPG"; } }

而且,这是我的.cshtml文件:

@model SoulMasters.Models.Game.Player @{ ViewBag.Title = "Game"; Layout = "~/Views/Shared/_GameLayout.cshtml"; var imagePath = @Model.ImagePath; } <fieldset> <legend>Player</legend> <div class="display-field"> @Html.DisplayFor(model => model.ImagePath) </div> @imagePath <div style="padding:10px;"> <img src=@imagePath alt="Sample Image" width="300px" /> <img alt="asd" > model.ImagePath; </img> </div> </fieldset>

结果是简单的文字:

~/Content/img/sql_error.JPG ~/Content/img/sql_error.JPG Sample Image asd model.ImagePath;

此外,我尝试了以下行,它的工作原理:

<img src="~/Content/img/sql_error.JPG" alt="Sample Image" width="300px" />

如何从路径显示该图像?
根据设置,图像路径可能不同吗?还有其他想法吗?

关于剃刀语法是如何工作的,我现在还没有真正理解它.

如何在ASP.NET MVC 4中使用Razor视图展示URL路径下的图片?

在你的视图中尝试这样

<img src= "@Url.Content(Model.ImagePath)" alt="Image" />

本文共计297个文字,预计阅读时间需要2分钟。

如何在ASP.NET MVC 4中使用Razor视图展示URL路径下的图片?

javapublic class Player { public String ImagePath { get { return ~/Content/img/sql_error.JPG; } } 这是我的.cs文件: @model SoulMasters.Models.Game.Player @{ ViewBag.Title=Game; Layout=~/Views/Shared/_Layout.cs; }

我有以下型号:

public class Player { public String ImagePath { get { return "~/Content/img/sql_error.JPG"; } }

而且,这是我的.cshtml文件:

@model SoulMasters.Models.Game.Player @{ ViewBag.Title = "Game"; Layout = "~/Views/Shared/_GameLayout.cshtml"; var imagePath = @Model.ImagePath; } <fieldset> <legend>Player</legend> <div class="display-field"> @Html.DisplayFor(model => model.ImagePath) </div> @imagePath <div style="padding:10px;"> <img src=@imagePath alt="Sample Image" width="300px" /> <img alt="asd" > model.ImagePath; </img> </div> </fieldset>

结果是简单的文字:

~/Content/img/sql_error.JPG ~/Content/img/sql_error.JPG Sample Image asd model.ImagePath;

此外,我尝试了以下行,它的工作原理:

<img src="~/Content/img/sql_error.JPG" alt="Sample Image" width="300px" />

如何从路径显示该图像?
根据设置,图像路径可能不同吗?还有其他想法吗?

关于剃刀语法是如何工作的,我现在还没有真正理解它.

如何在ASP.NET MVC 4中使用Razor视图展示URL路径下的图片?

在你的视图中尝试这样

<img src= "@Url.Content(Model.ImagePath)" alt="Image" />