在itemAdding事件接收器中返回List的当前URL?

EST*_*EST 0 visual-studio-2010 sharepoint-2010

ItemAdding事件接收器中,我想List使用c#返回当前的完整URL .我该怎么做呢?

我试过了:

string currentURL = SPContext.Current.File.Url;
Run Code Online (Sandbox Code Playgroud)

但是,这会在运行时返回"对象引用未设置为对象的实例".

基本上寻找指导我需要做什么来返回此事件接收器中的当前URL?

谢谢

int*_*t32 5

您得到"对象引用未设置为对象的实例"错误,因为SPContext.Current为null.请尝试:

string url = properties.Web.Url + "/" + properties.ListItem.Url;
Run Code Online (Sandbox Code Playgroud)

关于SPContext.Current的一些注意事项 - 您可以在EventReceiver中使用SPContext,但是您必须将它保存在事件接收器的construstor中,类似于public MyItemReceiver(){current = SPContext.Current; 而不是使用这个保存的上下文.