标签: server.mappath

我怎样才能使用global.asax中的Server.MapPath()?

我需要用来Server.MapPath()组合我存储在的一些文件路径web.config.

但是,由于Server.MapPath()依赖于当前的HttpContext(我认为),我无法做到这一点.在尝试使用该方法时,即使它"可用",我也会遇到以下异常:

在此上下文中无法使用服务器操作.

是否有另一种方法可以将Web根目录相对目录映射~/App_Data/到完整的物理路径,例如C:\inetpub\wwwroot\project\App_data\

c# asp.net server.mappath application-start global-asax

117
推荐指数
2
解决办法
3万
查看次数

在ASP.NET MVC中的静态字段内使用Server.MapPath()

我正在构建一个ASP.NET MVC站点,我正在使用Lucene.Net进行搜索查询.我在这里问了一个关于如何在ASP.NET MVC应用程序中正确构建Lucene.Net用法的问题,并被告知最好的方法是将my声明IndexWriterpublic static,以便可以重用它.

以下是我的SearchController顶部的一些代码:

public static string IndexLocation = Server.MapPath("~/lucene");
public static Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
public static IndexWriter writer = new IndexWriter(IndexLocation,analyzer);
Run Code Online (Sandbox Code Playgroud)

由于writer是静态的,IndexLocation也必须是静态的.因此,编译器给出了以下错误Server.MapPath():

非静态字段,方法或属性'System.Web.Mvc.Controller.Server.get'需要对象引用

没有办法在静态字段中使用Server.MapPath()或类似的东西?我该如何解决这个错误?

c# asp.net-mvc static lucene.net server.mappath

114
推荐指数
2
解决办法
11万
查看次数

无法使用Server.MapPath

我必须做些什么来做Server.MapPath工作?
我有using System.Web;

还有什么?当我输入时Server,没有快速结果选项(智能)Server.

有帮助吗?

c# intellisense server.mappath visual-studio

106
推荐指数
3
解决办法
21万
查看次数

C#classlibrary中的Server.Mappath

我如何在C#类库类中使用server.mappath方法,该类充当我的MyLabbsite的BusinessLayer

c# server.mappath

54
推荐指数
4
解决办法
11万
查看次数

在asp.net mvc中映射物理文件路径

我试图从我的ASP.Net MVC控制器中的磁盘读取XSLT文件.我正在做的是以下内容:

string filepath = HttpContext.Request.PhysicalApplicationPath;
filepath += "/Content/Xsl/pubmed.xslt";
string xsl = System.IO.File.ReadAllText(filepath);
Run Code Online (Sandbox Code Playgroud)

但是,在forums.asp.net这个帖子的一半,有以下引用

HttpContext.Current是邪恶的,如果你在你的mvc应用程序中的任何地方使用它,你做错了,因为你不需要它.

虽然我没有使用Current,但我想知道在MVC中确定文件的绝对物理路径的最佳方法是什么?出于某种原因(我不知道为什么!)HttpContext对我来说不合适.

在ASP.Net MVC中是否有更好的(或推荐/最佳实践)方法从磁盘读取文件?

asp.net-mvc server.mappath filepath

41
推荐指数
3
解决办法
7万
查看次数

在MVC3中使用Server.MapPath

我有代码

string xsltPath = System.Web.HttpContext.Current.Server.MapPath(@"App_Data") + "\\" + TransformFileName
Run Code Online (Sandbox Code Playgroud)

它回来了

C:\inetpub\wwwroot\websiteName\SERVICENAME\App_Data\FileName.xsl

为什么我要获取ServiceController的路径SERVICENAME?我想要的App_Data路径

C:\inetpub\wwwroot\websiteName\App_Data\FileName.xsl

c# asp.net iis server.mappath asp.net-mvc-3

23
推荐指数
2
解决办法
5万
查看次数

C#单元测试:测试使用MapPath的方法

首先,我知道这个问题非常危险: 如何在C#中进行单元测试中的MapPath

但我希望它有一个不同的解决方案.我的问题如下:

在我的代码中,我有一个需要验证的对象.我正在为每个验证方法创建单元测试,以确保它正确验证.我正在创建模拟数据并将其加载到对象中,然后验证它.问题是在验证过程中,当发生错误时,会分配错误代码.此错误代码用于使用Server.MapPath从xml文件收集有关错误的信息.但是,在尝试获取xml文件时,会抛出异常,这意味着无法找到该文件.

由于MapPath在我的验证代码中,而不是我的单元测试,如何让我的单元测试识别路径?这个问题有意义吗?

错误行(在我的验证码中不是我的单元测试):

XDocument xdoc = XDocument.Load(HttpContext.Current.Server.MapPath("App_Data/ErrorCodes.xml"));
Run Code Online (Sandbox Code Playgroud)

简化:单元测试在我的程序中调用一个调用Server.MapPath然后失败的方法.

.net c# unit-testing server.mappath

19
推荐指数
2
解决办法
1万
查看次数

从工作线程使用MapPath

我有一个WCF服务方法,它在我从另一个方法旋转的工作线程中运行.
我需要将相对服务应用程序路径("〜/ Templates/a.template")映射到物理路径("D:\ Web\Templates\a.template"),但我无法使用HttpContext.Current.Server.MapPath因为HttpContext.Current在工人线程.我怎么能达到MapPath方法?

谢谢!安德烈

asp.net wcf server.mappath httpcontext

16
推荐指数
2
解决办法
3904
查看次数

当HTTPContext .Current为Nothing时,如何使用Server.MapPath

当我需要从Web服务器上的目录中删除一些图像文件时,我有一些代码可以正常工作:

Dim ImageURL As String = dsImages.Tables(0).Rows(iImgRow).Item("ImageURL")
Dim physicalName = Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdid, iImgID)
Run Code Online (Sandbox Code Playgroud)

..但是当以设定的时间间隔在单独的线程中运行的维护任务确定需要删除上述文件时,我遇到了问题:

Dim ImageURL As String = dsImage.Tables(0).Rows(i - 1).Item("ImageURL")
Dim iImgID As Integer = dsImage.Tables(0).Rows(i - 1).Item("ImageId")
Dim physicalName As String = HttpContext.Current.Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdID, iImgID)
Run Code Online (Sandbox Code Playgroud)

在后一种情况下,HttpContext.Current.Server.MapPath(ImageURL)的值为Nothing.

有没有办法获得这种情况的完整路径?

vb.net asp.net server.mappath httpcontext

16
推荐指数
2
解决办法
2万
查看次数

如何在类库项目中使用Server.MapPath

我有一个Web应用程序,它有许多类库项目.下面是一些示例代码.

public static class LenderBL
{
    static string LenderXml { get { return "MyPathHere"; } }

    public static LenderColl GetLenders()
    {
        var serializer = new XmlSerializer(typeof(LenderColl));

        using (XmlReader reader = XmlReader.Create(LenderXml))
        {
            return (LenderColl)serializer.Deserialize(reader);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我通常会使用Server.MapPath来获取属性LenderXml的路径,但是当我在类库中使用它时,它将返回父解决方案的路径,而不是类库项目的路径.

有没有办法获得类库项目本身的路径?

提前致谢.

c# asp.net server.mappath

10
推荐指数
2
解决办法
5万
查看次数