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
Server.MapPath does not exist in current context
提问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.MapPath
resides in System.Web
and both namespace and assembly are added into class with using System.Web
and System.Web.dll
is added to the prj.
我知道Server.MapPath
位于System.Web
并且命名空间和程序集都通过 using 添加到类中System.Web
并System.Web.dll
添加到 prj 中。
on Ctrl+ .. I'm getting Microsoft.SqlServer
as 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
Server
property is accessible from within controller. You can also find it in HttpContext.Current
provided that your app is executed inside asp.net environment.
Server
属性可从控制器内部访问。如果HttpContext.Current
您的应用程序在 asp.net 环境中执行,您也可以在其中找到它。