Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use response.redirect from inside a function defined in 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 default.aspx.cs file, In this function I am not able to use Response.Redirect("Error.aspx") to show user that error has been generated.

Below is example of 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 may function defined in Business and Datalayer where i want to trap the error and redirect user to the Error page.

like image 646
Learning Avatar asked Jan 23 '12 06:01

Learning


People also ask

How do I use response redirect?

Response. Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister. aspx" page and it has a button that redirects you to the "UserDetail.

How do I open a response redirect in a new tab?

When the Button is clicked, first the SetTarget JavaScript function is called inside which the target property of the Form is set to _blank and after that the Server Side function is executed. Inside the Button Click event handler, the Response. Redirect code is executed and the Page opens in new Tab.

Which of the following is used to redirect to different page without post back?

Server. Transfer() will perform a transfer to another page on the server-side.


2 Answers

HttpContext.Current.Response.Redirect("error.aspx");

to use it your assembly should reference System.Web.

like image 163
adt Avatar answered Sep 18 '22 10:09

adt


For a start, in one place you're trying to use:

response.redirect(...);

which wouldn't work anyway - C# is case-sensitive.

But the bigger problem is that normally Response.Redirect uses the Page.Response property to get at the relevant HttpResponse. That isn't available when you're not in a page, of course.

Options:

  • Use HttpContext.Current.Response to get at the response for the current response for the executing thread
  • Pass it into the method as a parameter:

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

(EDIT: As noted in comments, you also have a SQL Injection vulnerability. Please use parameterized SQL. Likewise showing exception messages directly to users can be a security vulnerability in itself...)

like image 40
Jon Skeet Avatar answered Sep 22 '22 10:09

Jon Skeet