Click to See Complete Forum and Search --> : [RESOLVED] 'Response' does not exist in the current context
toraj58
November 30th, 2008, 08:58 AM
i have added a class to App_Code and i want to call a static method(Check()) within it in all the .aspx pages; but i recieved mentioned errors.
the code i use is here:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
/// <summary>
/// Summary description for Auth
/// </summary>
public class Auth
{
public Auth()
{
//
// TODO: Add constructor logic here
//
}
public static void Check()
{
if ((string)Session["logged"] != true.ToString())
Response.Redirect("~/default.aspx");
}
}
i get these errors:
Error 3 The name 'Session' does not exist in the current context E:\ASP.NET-SITES\GIS\App_Code\Auth.cs 28 21 E:\ASP.NET-SITES\GIS\
and
Error 4 The name 'Response' does not exist in the current context E:\ASP.NET-SITES\GIS\App_Code\Auth.cs 29 13 E:\ASP.NET-SITES\GIS\
is there any alternative to Response.Redirect() and Server.Transfer() that i can use in my class?
also in my class how i can gain access to session objects?
dannystommen
December 1st, 2008, 05:31 AM
Change the class to
public class Auth: System.Web.UI.Page{
}
eclipsed4utoo
December 1st, 2008, 10:00 AM
also, your if statement could be this...
if (!Convert.ToBoolean(Session["logged"]))
Response.Redirect("~/default.aspx");
toraj58
December 2nd, 2008, 03:20 AM
i revised it:
public class Auth : System.Web.UI.Page
now i get these errors:
Error 2 An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Response.get' E:\ASP.NET-SITES\GIS\App_Code\Auth.cs 29 13 E:\ASP.NET-SITES\GIS\
and
Error 1 An object reference is required for the nonstatic field, method, or property 'System.Web.UI.Page.Session.get' E:\ASP.NET-SITES\GIS\App_Code\Auth.cs 28 32 E:\ASP.NET-SITES\GIS\
dannystommen
December 2nd, 2008, 03:43 AM
That's because Session and Response are non static.
The next solution will work
using System.Web.SessionState;
namespace MyWebsite{
public class Auth{
public static void Check(HttpSessionState session, HttpResponse response) {
if ((string)session["logged"] != true.ToString())
response.Redirect("~/default.aspx");
}
}
}
//I call this method in home.aspx pageload
namespace MyWebsite{
public partial class Home: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
Auth.Check(Session, Response);
}
}
}
toraj58
December 2nd, 2008, 04:58 AM
yes, i also wanted to qualify my class with namespace but already i have a lot of .cs file in my project without any namespace. if i want add name space to each one by one i think this approach is not logical. how i can add a namespace on top of all my classes at once? is it possible?
i am trying to enhance the code.
toraj58
December 2nd, 2008, 05:18 AM
thanks the problem resolved somewhat. but still i have no namespace at top of my classes.
i don't know why when i created the project visual studio 2005 did not added any namespace like when i create windows applications.
dannystommen
December 2nd, 2008, 05:37 AM
If you want to do this programmatically, your approach needs to look like this:
- find al files with *.cs
- Open that file with a StreamReader;
- Keep writing each line to a new StreamWriter until you find the line "public class", then first write your namespace.
Next code should work
DirectoryInfo dir = new DirectoryInfo("c:/temp");
FileInfo[] files = dir.GetFiles("*.cs");
foreach (FileInfo fi in files) {
StreamReader SR = new StreamReader(fi.FullName);
//remove '.cs'
string newFileName = fi.FullName.Remove(fi.FullName.Length - 3) + "2.cs";
StreamWriter SW = new StreamWriter(newFileName);
string line = SR.ReadLine();;
while (line != null) {
//remove spaces in front and end
string tempLine = line.Trim();
if (tempLine.Length >= 12) {
if (tempLine.Substring(0, 12) == "public class") {
SW.WriteLine("namespace MyNameSpace{");
}
else {
if (tempLine.Length >= 20) {
if (tempLine.Substring(0, 20) == "public partial class") {
SW.WriteLine("namespace MyNameSpace{");
}
}
}
}
SW.WriteLine(line);
line = SR.ReadLine();
}
//closing tag of namespace
SW.WriteLine("}");
SR.Close();
SW.Flush();
SW.Close();
}
dannystommen
December 2nd, 2008, 05:39 AM
Make sure that in your folder where your .cs files are, only files are without any namespace, otherwise a .cs file with namespace gets a second namespace.
Feel free to make a check if there is a namespace in the file, and if that's the case, don't add the line 'namespace MyNamespace' to the file
toraj58
December 2nd, 2008, 05:59 AM
thanks for your reply; yes your solution works.
i thought that somewhere in Visual Studio may exist some functionality for adding a namespace to files in the project.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.