An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'

StackOverflow https://stackoverflow.com/questions/23246419

  •  08-07-2023
  •  | 
  •  

Domanda

i 'm working with web app. i have found error during compile time. this error occurs from this code:

public static void SendRegistrationEmail(string name, string ref_no, string email)
    {
        MailMessage msg = new MailMessage();
        msg.IsBodyHtml = true;
        SmtpClient client = GetSmtpClient();
        string fromEmailAddress = ConfigurationManager.AppSettings["EASYTRACK_FromEmailAddress"];
        string appPath = ConfigurationManager.AppSettings["EASYTRACK_PathToApplication"];
        string subject = "EASYTRACK | User Registration";
        string body = "<html><body>Thank you " + name + " !" +
                    " for registering your details with Easy Track.<br/><br/>" +
                    "Please click the following link to activate your account" +
                    "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Register.aspx", appPath + "My_Account.aspx?Ref=" + ref_no) + "'>Click here to activate your account.</a><br/><br/>" +
                    "Thanks & Regards From Easy Track Team<br/></br></br><b>Note:</b>Your Account is activated after clicking this link.</body></html>";
        msg.From = new MailAddress(fromEmailAddress);
        msg.To.Add(new MailAddress(email));
        msg.Subject = subject;
        msg.Body = body;
        client.Send(msg);
    }

here error occurs from Request.Url.AbsoluteUri here i can not get this Request how ever at Page_Load i can get. what was issue that???

È stato utile?

Soluzione

The Page class instance has the Request property so that you can easily work with it.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.request%28v=vs.110%29.aspx

Your code being static method, instead, doesn't have access to Request property, since static methods do not have access to instance members.

Use HttpContext.Current.Request.Url instead of Request.Url to make your code more universal.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top