gpt4 book ai didi

.net - 名称 'HttpContext' 在 Razor 的当前上下文中不存在

转载 作者:行者123 更新时间:2023-12-05 06:31:40 27 4
gpt4 key购买 nike

我在这里感觉有点傻,因为我已经为此工作了几个小时。

我有一个简单的 Razor 页面,我想在其中检查文件是否存在。当我使用“HttpContext.Current.Server.MapPath”并运行页面时,出现“HttpContext 不存在”错误。

我还尝试了 HostingEnvironment.MapPath(在注释行 Using System.Web.Hosting 中可见),但这也不起作用。这对我来说都是全新的,所以可能是我遗漏了一些东西。据我所知,引用了正确的库。

    @inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
@model IEnumerable<SensorView>
@using System.IO;
@using System.Web;

<div class="container-fluid">
<div class="row">
@{
if (Model != null)
{
foreach (SensorView sensor in Model)
{
<div class="col-sm-6 col-lg-4">
<div class="thumbnail">
@{
@*<img src="@Url.Content(string.Format("~{0}.jpg", sensor.Image))" alt="@sensor.Image" class="sensor-image" />*@

var absolutePath = HttpContext.Current.Server.MapPath(string.Format("~{0}.jpg", sensor.Image));
//var absolutePath = HostingEnvironment.MapPath(string.Format("~{0}.jpg", sensor.Image));
if (File.Exists(absolutePath))
{
<img src="@Url.Content(string.Format("~{0}.jpg", sensor.Image))" alt="@sensor.Image" class="sensor-image" />
}
else
{
<img src="@Url.Content(@"images/sensoren/320x150.png")" alt="@sensor.Image" class="sensor-image" />
}
}
<div class="caption">
<h4>
<a asp-action="Detail" asp-controller="Home" asp-route-SensorId="@sensor.ID" role="link">@sensor.Title</a>
</h4>
<p>
@sensor.DescriptionShort
</p>
<a asp-action="Detail" asp-controller="Home" asp-route-SensorId="@sensor.ID" class="btn btn-info _trMoreInformation" role="button">@Localizer["MoreInformation"]</a>
</div>
</div>
</div>
}
}
}
</div>
</div>

我认为这会很简单,但如前所述,我已经为此工作了几个小时,但没有任何进展。

可能是天气太热了;-)

无论如何,我尝试协助的是检查文件是否存在,如果不存在则显示默认图像。

感谢任何帮助。

[更新]

项目结构如下图所示。 enter image description here

我最终得到了以下代码。这对我有用。

    @inject Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
@model IEnumerable<SensorView>
@using System.IO;
@using System.Web;


<div class="container-fluid">
<div class="row">
@{
if (Model != null)
{
foreach (SensorView sensor in Model)
{
<div class="col-sm-6 col-lg-4">
<div class="thumbnail">
@{
@*<img src="@Url.Content(string.Format("~{0}.jpg", sensor.Image))" alt="@sensor.Image" class="sensor-image" />*@


if (HostingEnvironment.ContentRootFileProvider.GetFileInfo(string.Format("wwwroot{0}.jpg", sensor.Image)).Exists)
{
<img src="@Url.Content(string.Format("~{0}.jpg", sensor.Image))" alt="@sensor.Image" class="sensor-image" />
}
else
{
<img src="@Url.Content(@"/images/sensoren/320x150.png")" alt="@sensor.Image" class="sensor-image" />
}
}
<div class="caption">
<h4 style="min-height:40px;">
<a asp-action="Detail" asp-controller="Home" asp-route-SensorId="@sensor.ID" role="link">@sensor.Title</a>
</h4>
<div style="min-height:80px;">
<p>
@sensor.DescriptionShort
</p>
</div>
<a asp-action="Detail" asp-controller="Home" asp-route-SensorId="@sensor.ID" class="btn btn-info _trMoreInformation" role="button">@Localizer["MoreInformation"]</a>
</div>
</div>
</div>
}
}
}
</div>
</div>

最佳答案

我认为正确的语法是:

var absolutePath = Context.Server.MapPath(string.Format("~{0}.jpg", sensor.Image));

也许 this document将帮助您找到最合适的方法。

关于.net - 名称 'HttpContext' 在 Razor 的当前上下文中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51727896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com