C# Server.MapPath 在当前上下文中不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19195663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:21:58  来源:igfitidea点击:

Server.MapPath does not exist in current context

c#.netasp.net-mvc

提问by panjo

Inside my MVC4 project I have the namespace

在我的 MVC4 项目中,我有命名空间

prj.MVC4.Controllers

prj.MVC4.Controllers

where I'm using Server.MapPath(..)without problem and on

我在哪里使用Server.MapPath(..)没有问题

prj.MVC4.Models

prj.MVC4.Models

Server.MapPath(...)does not exist on current context.

Server.MapPath(...)在当前上下文中不存在。

I'm aware that Server.MapPathresides in System.Weband both namespace and assembly are added into class with using System.Weband System.Web.dllis added to the prj.

我知道Server.MapPath位于System.Web并且命名空间和程序集都通过 using 添加到类中System.WebSystem.Web.dll添加到 prj 中。

on Ctrl+ .. I'm getting Microsoft.SqlServeras suggested namespace to add.

Ctrl+ 上.。我正在 Microsoft.SqlServer按照建议的命名空间添加。

How to fix this?

如何解决这个问题?

采纳答案by MichaC

Server is a property of the controller, to access it elsewhere while running a web application you can use

服务器是控制器的一个属性,在运行你可以使用的 web 应用程序时在其他地方访问它

System.Web.HttpContext.Current.Server

回答by Zbigniew

Serverproperty is accessible from within controller. You can also find it in HttpContext.Currentprovided that your app is executed inside asp.net environment.

Server属性可从控制器内部访问。如果HttpContext.Current您的应用程序在 asp.net 环境中执行,您也可以在其中找到它。