- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里感觉有点傻,因为我已经为此工作了几个小时。
我有一个简单的 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>
我认为这会很简单,但如前所述,我已经为此工作了几个小时,但没有任何进展。
可能是天气太热了;-)
无论如何,我尝试协助的是检查文件是否存在,如果不存在则显示默认图像。
感谢任何帮助。
[更新]
我最终得到了以下代码。这对我有用。
@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/
我是 HangFire 的初学者,期待使用 HangFire 每月在我的网络应用程序中调用一些操作。但是这些操作需要 HttpContext。 那么我的问题是:有没有办法在 HangFire 项目中添
是 HttpContext.Current.User 在全局 asax 中与 不同HttpContext.User 在 Action 方法中?我为用户分配了一些角色,但他们似乎迷路了。 下面的代码显示
我不明白 HttpContext.Handler 和 HttpContext.CurrentHandler 之间有什么区别。 任何人? 最佳答案 微软: CurrentHandler 属性引用的当前正
这两种获取当前 asp.net 请求异常的方法之间有什么实际区别吗? MSDN 说 HttpContent.Error 返回第一个错误,而 GetLastError() 显然是最后一个错误,但我似乎无
我正在使用 httphandler。在 ProcessRequest 中传递的 HttpContext 是否引用了 HttpContext.Current?使用它们有什么不同吗? 最佳答案 是的,两者
我目前正在处理一些遗留代码,其中 HttpContext.Current.User 属性在 MVC Controller 方法中用于对当前用户执行一些基本授权。通过阅读文档,还有一个 HttpCont
我们正在迁移应用程序以使用 IIS7 集成模式。在设计为在 HTTP 请求上下文或不在上下文中工作的库代码中,我们通常有这样的代码: if (HttpContext.Current != null &
我想知道 Web API Controller 中的 HttpContext.Request.Path 和 HttpContext.Request.PathBase 有什么区别?我阅读了文档但不明白有
我真的无法找出 C#/.NET 中这两种方法之间的真正区别。 事实上他们应该做同样的 Action ! 最佳答案 第一个包含一个安全的读/写存储位置,可以在整个 HTTP 请求中使用。例如,您可以使用
想知道 - HttpContext.Response.Cache 和 HttpContext.Current.Cache 对象有什么区别?以及在 Asp.net MVC Web 应用程序中应该使用什么
我的 Controller 有这样声明的用法(不确定顺序是否重要) using System; using System.Collections.Generic; using System.Linq;
我的团队正在开发一个基于 ASP.NET Core Web 应用程序 (.NET Framework) 模板构建的新项目。由于遗留原因,我们有另一个项目构建在 System.Web 上,并通过 Htt
如果我想存储一些对象以在页面和 session 之间共享,我应该使用哪一个? HttpContext.Current.ApplicationInstance.Application 或 HttpCon
这 2 个似乎有些含糊不清。我在整个项目中交替使用它们,唯一的原因是我不知道何时使用其中一个。 在什么情况下一个为真而另一个为假? 如果我只使用 ASP.NET Identity 来对用户进行身份验证
我正在开发一个 ASP.NET web 应用程序,我想实现缓存,所以我想知道 HttpContext.Current.Cache.Insert 和 HttpContext.Current 之间的区别.
这两个属性有什么区别? 我可以使用 HttpContext.Items 而不是 HttpContext.Features 在中间件之间共享数据。我看到的唯一区别是我告诉 Items 一个键,它给了我一
微软的HttpContext.Current.Request.ServerVariables[“REMOTE_ADDR”]正在为远程客户端返回ipv6。但是,我需要将此数据用于一个会话日志表,其中的C
if (HttpContext.Current.Request.Cookies.AllKeys.Contains("myCookie") && !String.IsNullOrEmpty(Ht
我有一个项目作为引用添加到 System.Web。 但是,它似乎无法获取 HttpContext。这样做: Imports System.Web _ApplicationBase = HttpCont
我创建了这个类来从请求中获取 Header 值。 public class AuthenticationHeader { private static IHttpContextAccesso
我是一名优秀的程序员,十分优秀!