How can I use response.redirect from inside a function defined in a class file in C # 3.0

I have a simple function GetPageName(String PageFileName, String LangCode)defined inside a class file. I call this function from a file default.aspx.cs. In this function, I cannot use Response.Redirect("Error.aspx")to show the user that this error has been generated.

Below is a sample code

public static string GetPageName(String PageFileName, String LangCode)
{
     String sLangCode = Request("Language");
     String pgName = null;
     if ( sLangCode.Length > 6)
     {
        Reponse.Redirect("Error.aspx?msg=Invalid Input");
     }
     else
     {
         try
         {            
             String strSql = "SELECT* FROM Table";

             Dataset ds = Dataprovider.Connect_SQL(strSql);

         }
         catch( Exception ex)
         {
            response.redirect("Error.aspx?msg="+ex.Message);
         }
     }
     return pgName;
}

I have a function defined in Business and Datalayer where I want to catch an error and redirect the user to the Error page.

+5
source share
2 answers
HttpContext.Current.Response.Redirect("error.aspx");

To use it, your assembly must reference System.Web.

+14
source

For starters, in one place you are trying to use:

response.redirect(...);

which won't work - C # is case sensitive.

, Response.Redirect Page.Response, HttpResponse. , , .

:

  • HttpContext.Current.Response,
  • :

    // Note: parameter names changed to follow .NET conventions
    public static string GetPageName(String pageFileName, String langCode,
                                     HttpResponse response)
    {
        ...
        response.Redirect(...);
    }
    

(EDIT: , SQL Injection. , SQL. , ...)

+4

All Articles