【问题标题】:Sharepoint 2010 redirect from event receiver (after creating site)Sharepoint 2010 从事件接收器重定向(创建站点后)
【发布时间】:2012-05-05 11:04:16
【问题描述】:

我的问题是: 在用户创建站点后的 Sharepoint 2010 中,首先我需要将用户重定向到我的自定义页面,而不是将用户重定向到新站点的 url。

我为网络创建了事件接收器,并在使用 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 为空。

我在 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 不为空(我为 WebProvisioned 属性设置了 Synchronous)

<Synchronization>Synchronous</Synchronization>

但在这种情况下,我收到错误 “无法评估表达式,因为代码已优化或本机框架位于调用堆栈顶部”。

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

但是在这种情况下也没有什么是行不通的。

如果有任何帮助,我将不胜感激!

【问题讨论】:

  • 如果你能够正确获取上下文,那么你可以试试oContext.Response.Redirect("url", false),而不是使用SPUtility.Redirect,第二个参数(false)很重要

标签: asp.net sharepoint redirect sharepoint-2010 eventreceiver


【解决方案1】:

你好,你可以试试这个从事件接收器重定向到你的自定义页面

    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/

如果工作,请告诉我。 谢谢

【讨论】:

  • 是的,我试着去做。但是重定向不起作用,但我也没有收到错误。用户重定向到新站点的 url。我还添加了下一行代码:properties.Cancel = true; this.EventFiringEnabled = false;但这无济于事。
【解决方案2】:

您需要添加一个构造函数来检索当前的 httpcontext,否则您的上下文将始终为 null。

参见以下示例:http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/8cfcb299-9a55-4139-86ec-0b53b2922a54/

【讨论】:

  • 我通过使用第二个代码块使 HttpContext 不为空。但是,当我尝试使用此 HttpContext(用于重定向)时,出现以下错误:“无法评估表达式,因为代码已优化或本机框架位于调用堆栈之上”。
猜你喜欢
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多