【发布时间】:2014-09-18 19:53:38
【问题描述】:
我正在尝试获取类的类 VirtualPath。
当类被实例化时,我可以调用this.AppRelativeVirtualPath;(或者,在VB中Me.AppRelativeVirtualPath)这会给我“~/content/page.aspx”。
现在,我需要调用 THE CLASS,而不是实例来获取该路径。像这样的:
public class Page : System.Web.UI.Page
{
public static string GetPath() { return Page.AppRelativeVirtualPath; }
}
但我做不到,因为“AppRelativeVirtualPath”是非共享方法。 调用它的方法是什么?
我需要能得到与此相同结果的东西:http://msdn.microsoft.com/pt-br/library/system.web.ui.templatecontrol.apprelativevirtualpath(v=vs.110).aspx
在 TIM 的回答之后进行编辑
我用他的代码写的具体类是这样的:
Namespace Web.Pages
Public Class Dir
Inherits System.Web.UI.Page
Public Shared Function GetPath() As String
Dim page As Web.Pages.Dir = TryCast(System.Web.HttpContext.Current.Handler, Web.Pages.Dir)
If (Page Is Nothing) Then Return "" Else Return page.AppRelativeVirtualPath
End Function
End Class
End Namespace
还有(我想解决的问题):
Namespace Web.Pages
Public Class MainPage
Inherits System.Web.UI.Page
Public ReadOnly Property LinkToDir() As String
Get
Return Web.Pages.Dir.GetPath()
End Get
End Property
End Class
End Namespace
看,上下文在“MainPage”上,它调用“Dir”页面上的静态方法(“Dir”)返回它的 ASP 页面虚拟地址(即“~/content/dir.aspx” )。
我怎样才能做到这一点?
【问题讨论】: