Razor的当前上下文中不存在名称“ HttpContext”

时间:2018-08-07 13:22:31

标签: .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>


I thought it would be straightforward but as said, I have been working on this for a few hours now without any progress. 

It could be the hot weather ;-)

Anyway, what I try to accomplice is check if a file exists, if not show a default image. 

Any help is appreciated.

[Update]

The project structure is visible in the picture below.[![enter image description here][1]][1]

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

    @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>

2 个答案:

答案 0 :(得分:0)

我相信正确的语法是:

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

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

答案 1 :(得分:0)

  1. 如果您使用的是Razor Pages Web App,您会错过User and Article interfaces,并且需要在cshtml文件顶部添加@page指令,以使其编译为从IRazorPage间接继承的类。结果,您将拥有HttpContext属性。
  2. 要获取@page,可以通过HostingEnvironment注入它,从而可以通过@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment检查文件是否存在。
  3. 提醒一下,如果要在项目的根路径下提供静态文件,请不要忘记使用ContentRootFileProvider.GetFileInfo(subfilepath).Exists