非静态字段,方法或属性需要对象引用,System.Web.UI.Page.Request.get'

时间:2014-04-23 13:50:19

标签: c# asp.net-4.0

我正在使用网络应用。我在编译期间发现了错误。此代码中出现此错误:

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);
    }

这里发生错误来自Request.Url.AbsoluteUri这里我无法得到这个请求如何在Page_Load我能得到。是什么问题???

1 个答案:

答案 0 :(得分:6)

Page类实例具有Request属性,因此您可以轻松使用它。

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

您的代码是静态方法,而不能访问Request属性,因为静态方法无法访问实例成员。

使用HttpContext.Current.Request.Url代替of Request.Url,使您的代码更具通用性。