FBLover2011
FBLover2011

Reputation: 271

Can a normal Class use Response.Redirect to a Uri?

I have the below code but hitting an error saying "Response is not available in this context." What is wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

public class Class1
{
    public Class1()
    {
    }

    public void MoveToSite(string url)
    {
        System.Web.UI.Page Resp = new Page();
        Resp.Response.Redirect(url);
    }
}

Upvotes: 0

Views: 628

Answers (2)

Sjoerd
Sjoerd

Reputation: 75619

The Response object of a Page is an instance which represents the current HTTP response. It is passed to a page when it is created by ASP.NET. If you create a new page, it does magically get the Request property, somebody has to set it.

You can access the current request like this:

HttpContext.Current.Response.Redirect(...)

But it may be better to pass the Response object to your Class1 when constructing a new object of type Class1.

Upvotes: 2

Dennis Traub
Dennis Traub

Reputation: 51644

Try to access the Response object via HttpContext.Current.Response

Upvotes: 0

Related Questions