Response.Redirect扩展方法中的HttpContext.Current为null

时间:2018-03-02 07:08:09

标签: asp.net

我在框架4.6.1上有一个asp.net项目。 HttpContext.Current在正常流程中很好。但是,如果我使用Response.Redirect“Extension”方法,HttpContext.Current在那里为空。

对于正常的Response.Redirect,它运行正常。我已经应用了不同的解决方案,比如没有async / await方法等检查它,但行为是一样的。

任何想法?

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int N, S, C;


int checkSum(int index, int s, vector<int>& a, vector< vector<int> >& dP) {

    if (dP[index][s] != -1)
        return dP[index][s];

    int maxNums = 0;    // size of maximum subset array

    for (int i = index; i < N; i++) {

        int newSum = s - a[i];
        int l = 0;

        if (newSum == 0) {
            l = 1;
        } if (newSum > 0) {

            if (i < (N-1)) {    // only if we can still fill up sum
                l = checkSum(i + 1, newSum, a, dP);

                if (l > 0)      // if it is possible to create this sum
                    l++;        // include l in it
            } else {
                // l stays at 0 for there is no subset that can create this sum
            }

        } else {
            // there is no way to create this sum, including this number, so skip it;
            if (i == (N-1))
                break;      // don't go to the next level

            // and l stays at 0
        }

        if (l > maxNums) {
            maxNums = l;
        }
    }

    dP[index][s] = maxNums;
    return maxNums;
}


int main() {

    int t;
    cin >> t;

    while (t--) {
        cin >> N >> S >> C;
        vector<int> a(N);

        for (int i = 0; i < N; i++)
            cin >> a[i];

        vector< vector<int> > dP(N, vector<int>(S + C + 2, -1));

        bool possible = false;

        for (int i = 0; i <= C; i++) {
            int l = checkSum(0, S-i, a, dP);
            int m = checkSum(0, S+i, a, dP);

            if ( (l > 0 && l >= i) || (m > 0 && m >= i) ) {

                cout << "TRUE" << endl;
                possible = true;
                break;
            }

        }

        if (!possible)
            cout << "FALSE" << endl;
    }

    return 0;
}

2 个答案:

答案 0 :(得分:2)

了解您所称的位置非常重要。如果没有调用它的页面,则yes为null,并且为null,因为没有页面可以写入重定向命令(以及其他命令)。

例如,如果从新线程(而不是页面)调用它,则为null。

如果您从global.asax的某个部分调用此错误,也可能会收到此错误。

答案 1 :(得分:0)

Joel Harkes 评论解决了我的问题

  

为什么不使用:这个HttpContext上下文而不是这个HttpResponse   反模式响应:HttpContext.Current?

创建HttpContext的扩展方法解决了这个问题。

public static void Redirect(this HttpContext context, string url, string target, string windowFeatures)
{

    if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))
    {
        context.Response.Redirect(url);
    }
    else
    {

        Page page = (Page)context.CurrentHandler;//HttpContext.Current.Handler;

        if (page == null)
        {
            throw new InvalidOperationException("Cannot redirect to new window outside Page context.");
        }
        url = page.ResolveClientUrl(url);

        string script;
        if (!String.IsNullOrEmpty(windowFeatures))
        {
            script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
        }
        else
        {
            script = @"window.open(""{0}"", ""{1}"");";
        }
        script = String.Format(script, url, target, windowFeatures);
        ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
    }
}
相关问题