2012-05-05 91 views
1

我的问题是重定向: 2010年SharePoint用户后创建网站,首先我需要用户重定向到我的自定义网页,而不是用户重定向到新的网站网址。SharePoint 2010中从事件接收器(创建网站后)

我创建的事件reciever网页和创建到我的网页上重定向使用SPUtility.Redirect()后试图网站:

public class MySiteEventReceiver : SPWebEventReceiver 
{ 
    public override void WebProvisioned(SPWebEventProperties properties) 
    { 
     var web = properties.Web; 
     base.WebProvisioned(properties); 

     string url = web.Site.Url + "/_LAYOUTS/MyPage.aspx"; 
     SPUtility.Redirect(url, SPRedirectFlags.CheckUrl, HttpContext.Current); 
    } 
} 

但始终HttpContext.Current为null。

我一直在寻找我的问题在intrnet的解决方案。而且我发现它:http://www.sharepointkings.com/2008/05/httpcontext-in-eventhandler.html

我做到了:

public class MySiteEventReceiver : SPWebEventReceiver 
{ 
    private static HttpContext oContext; 

    public SubSiteEventReceiver() : base() 
    { 
     if (oContext == null) 
     { 
      oContext = HttpContext.Current; 
     } 
    } 
    public override void WebProvisioned(SPWebEventProperties properties) 
    { 
     var web = properties.Web; 
     base.WebProvisioned(properties); 

     string url = web.Site.Url + "/_LAYOUTS/MyPage.aspx"; 
     SPUtility.Redirect(url, SPRedirectFlags.CheckUrl, oContext); 
    } 
} 

之后的HttpContext是不是空

<Synchronization>Synchronous</Synchronization> 

(我的WebProvisioned性能同步设置),但在这种情况下,我得到错误“无法评估表达式,因为代码已经优化或者本机框架位于调用堆栈之上”。

我也试图去重定向的另一种方式。 http://sharesilver.wordpress.com/2011/07/24/sharepoint-bugs-1-item-deleting-event-receiver/

但在这种情况下也没有什么不工作。

我将是任何试图帮助感激不尽!

+0

如果你能正确地获取上下文,然后,而不是使用SPUtility.Redirect的,你可以尝试oContext.Response.Redirect(“URL”,假的),第二个参数(假)很重要 – Mac

回答

0

嗨u能尝试一下本作由事件接收器重定向到您的自定义页面

properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl; 
    properties.RedirectUrl = "/_layouts/EventReceiver/CustomError.aspx"; 

有关详情,请这个网址

http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-redirect-to-a-custom-page-for-event-receiver-in-sharepoint-2010/

让我知道你的工作。 感谢

+0

是的,我尝试去做。但重定向不起作用,但我也没有得到错误。用户重定向到新网站的网址。 我还添加了下面的代码行: properties.Cancel = true; this.EventFiringEnabled = false; 但它没有帮助。 –

+0

你们每个人都得到这个工作吗? – ben

0

您需要添加一个构造函数来获取当前的HttpContext,否则你的上下文将永远是空。

请看下面的例子:http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/8cfcb299-9a55-4139-86ec-0b53b2922a54/

+0

我通过使用第二个代码块使HttpContext不为空。但是,当我尝试使用此HttpContext(用于重定向)时,出现跟随错误:“无法评估表达式,因为代码已优化或本机框架位于调用堆栈之上”。 –

相关问题