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

标签 .net io asp.net-core-mvc httpcontext

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

我有一个简单的 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/

相关文章:

c# - 完全用 C# 编写的 BitTorrent 客户端?

c++ - 尝试使用 Win32 线程进行异步 I/O

asp.net-web-api - MVC 结合 ASP.NET Core 中的 Web API,但是 View 在哪里?

c# - ASP.NET Core 2.1 - 身份验证 cookie 已删除,但用户仍可登录而无需重定向到外部登录

.net - 想在Core Project中放一个4.6目录

c# - 从 C header 自动创建 C# 包装器?

c# - 以编程方式选择 DataGridView 中的一行

c# - 索引超出范围且数据少于 6 行

java - 读取/写入文件逐行报告位置

string - Haskell- IO String 获取多行