- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 T4 模板获取 Views 文件夹中的文件夹名称,但它不断给我以下错误:
错误 3 编译转换:名称“服务器”在当前上下文中不存在 c:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\StronglyTypedViews.tt 20 47
错误 4 命名空间不直接包含字段或方法等成员 C:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\StronglyTypedViews.cs 1 1 LearningMVC
这是T4模板:
<#@ template language="C#" debug="True" hostspecific="True" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Web" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Web" #>
using System;
namespace StronglyTypedViews
{
<#
string[] folders = Directory.GetDirectories(Server.MapPath("Views"));
foreach(string folderName in folders)
{
#>
public static class <#= folderName #> { }
<# } #>
}
<#@ template language="C#" debug="True" hostspecific="True" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Web" #>
<#@ assembly name="System.Web.Mvc" #>
<#@ import namespace="System.Web.Mvc" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Web" #>
using System;
namespace StronglyTypedViews
{
<#
string viewsFolderPath = @"C:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\";
string[] folders = Directory.GetDirectories(viewsFolderPath + "Views");
foreach(string folderName in folders)
{
#>
public static class <#= System.IO.Path.GetFileName(folderName) #> {
<#
foreach(string file in Directory.GetFiles(folderName)) {
#>
public const string <#= System.IO.Path.GetFileNameWithoutExtension(file) #> = "<#= System.IO.Path.GetFileNameWithoutExtension(file).ToString() #>";
<# } #>
<# } #>
}
}
最佳答案
T4 模板在 Visual Studio 创建的临时上下文中执行,并且完全在您的 Web 应用程序之外。该临时上下文用于生成输出文本文件。它在任何方面都不是 Web 应用程序,并且与您正在创作的 Web 应用程序无关。因此,System.Web.HttpContext 没有分配任何值,并且无法调用 MapPath()。
Environment.CurrentDirectory 也没有太大帮助,因为模板是在某个临时文件夹中执行的。
你能做什么?如果您可以使用绝对路径,请继续这样做。否则,在 <#@ template#> 指令中添加 hostspecific 属性将允许您访问 Host 变量及其 ResolvePath()方法。 ResolvePath 允许您解析相对于 TT 文件本身的路径。
例如(example.tt):
<#@ template language="C#" hostspecific="True" #>
<#@ output extension=".cs" #>
// <#=Host.ResolvePath(".")#>
// C:\Users\myusername\Documents\Visual Studio 2008\Projects\MvcApplication1\MvcApplication1\.
关于T4 模板和 Server.MapPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2044327/
谁能解释一下 Server.MapPath(".")、Server.MapPath("~")、Server.MapPath(@"之间的区别\") 和 Server.MapPath("/")? 最佳答案
Server.MapPath() 和 HostingEnvironment.MapPath() 有区别吗? Server.MapPath() 是否比 HostingEnvironment.MapPat
我想使用Server.MapPath()方法来映射虚拟目录 我创造了它的物理路径。 问题是.net环境无法识别Server.MapPath()。 Google告诉我,我应该使用System.Web使用
我的文件结构设置如下: ServerRoot applicationRoot filePage.aspx files chart.png 我的应用程序页
我的业务层创建文件并需要将它们保存在我的 asp.net mvc 4 网络前端的 App_Data 文件夹中。 我可以在业务层使用Server.MapPath 来获取App_Data 文件夹的物理路径
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章ASP的Server.MapPath()不同参数返回路径总结由作者收集整
我正在尝试使用 T4 模板获取 Views 文件夹中的文件夹名称,但它不断给我以下错误: 错误 3 编译转换:名称“服务器”在当前上下文中不存在 c:\Projects\LearningASPMVC\
我使用 Server.MapPath 访问服务器上的一些图像。我得到了正确的路径,但无法打开它。我得到的错误是: Not allowed to load local resource: d:/Proj
所以我有一个网络应用程序。 在 IIS 中,我有一个站点 WEBSITE,我的应用程序是从 WEBSITE 拥有的子站点 APPLICATION 运行的。 机器上的物理路径与返回的 HostingEn
我对 server.mappath 有点困惑哪个是正确的,这两个之间有什么区别 FileUpload1.saveAs(Server.MapPath("~/User/images/")+"ankush.
所以我正在读一本关于 asp.net 安全的书。其中一个部分是:如何防止目录遍历文件名(被黑的文件名)。 所以这行代码是: string fullPath = Server.MapPath(Syste
我有一个图像存储在服务器上的位置: C:\inetpub\wwwroot\imageupload\images 在我的代码中,我想使用文件夹 images 中的图像,所以我尝试如下: Server.M
我有一个 asp.net MVC 应用程序,我想获取远程地址以访问某个文件。 在我的 img 标签中,我使用了 ~/ 截至 在这种情况下,波浪号将被解释。 但我想通过 javascript 创建标签,
我有一个 MVC 应用程序并部署到服务器,我只有 ftp 访问权限,应用程序的名称是 test.foo.com。有一个后端,用户可以将图片上传到应用程序。一切都很好。代码如下: //in web co
我有一个 Web 应用程序,其中有许多类库项目。下面是一些示例代码。 public static class LenderBL { static string LenderXml { get
我有一个方法。从硬盘中检索文档。我无法通过单元测试对此进行测试。它总是抛出异常无效的空路径或其他东西。如何测试。我临时为单元测试创建了 session 。但我不能为这个 Server.MapPat
在我的 MVC4 项目中我有命名空间 prj.MVC4.Controllers 在我使用 Server.MapPath(..) 的地方没有问题并且在 prj.MVC4.Models Server.Ma
我有一个 C# 模型类,我试图访问一个 .cshtml 页面,它应该是一个电子邮件格式模板。我正在使用以下代码: string body = string.Empty; using (StreamRe
我有一个包含少数项目的 Visual Studio 2008 解决方案。一个项目包含一个 WCF Service我正在部署。该 WCF 服务引用了其他项目之一中的一些代码。该代码试图读取 WCF 项目
我注意到 Server.MapPath() 中有些奇怪的地方。如果我有一个末尾有空格的文件夹,我会得到: HttpException: Failed to map the path. 这很好用:Ser
我是一名优秀的程序员,十分优秀!