Search code examples
.netioasp.net-core-mvchttpcontext

The name 'HttpContext' does not exist in the current context in Razor


I feel a little stupid here, because I have been working on this for some hours now.

I got a simple Razor-page where I want to check if a file exists. When I use "HttpContext.Current.Server.MapPath" and I run the page, I get the "HttpContext does not exist"-error.

I also tried HostingEnvironment.MapPath (as is visible in the commented line, Using System.Web.Hosting), but that didn't work either. This is all quite new to me, so it could be I'm missing something. As far as I know, referenced the correct libraries.

    @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

I ended up with the following code. This works for me.

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

Solution

    1. If you are using a Razor Pages Web App, you missed @page and need add @page directive on the top of the cshtml file to make it compiled as a class inherits indirectly from IRazorPage . As a result ,you will have a HttpContext Property .
    2. To get a HostingEnvironment, you can inject it by @inject Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment, thus you can check whether the file exist via ContentRootFileProvider.GetFileInfo(subfilepath).Exists .
    3. Just as a reminder , if you want to serve static files under the root path of project , don’t forget to UseStaticFiles(staticFileOpts) .