将数据绑定到.ashx文件

时间:2014-11-10 18:44:17

标签: asp.net visual-studio-2010 visual-studio ashx generic-handler

我正在构建一个SMS Getaway,我正在使用SMS Enabler。所以这是我的例子,一个人通过短信发送代码。此代码在SQL Server数据库中循环,并检查此列中是否存在此代码,假设在数据库TEST,表TAGLE1,列CODES上。如果此人在此列中存在此代码,则SMS启动器会回复他是赢家的代码,否则代码不存在则会回复“再试一次”。我只能通过.ashx文件而不是.aspx文件来做到这一点 这是我的代码:

public class sms : IHttpHandler {

 // This method is called each time a new SMS message arrives
 public void ProcessRequest(HttpContext context) {
   // form variables (contain all sms message information)
   var sms = context.Request.Form;

   // sender's number
   string senderNumber = sms["sender"];
   // sms message text
   string messageText = sms["text"];
   // SMS center timestamp in UTC. You can consider this
   // as the date and time when the sender sent the message.
   DateTime sentTime = DateTime.ParseExact(sms["scts"],
         "yyyy'-'MM'-'dd HH':'mm':'ss", null);
   // SMS center number (not supported when using SMS Enabler 
   // with a Nokia phone)
   string smscNumber = sms["smsc"];
   // Tag value. You can define this in SMS Enabler's settings, 
   // and use it to pass additional information.
   string tag = sms["tag"];

   /* TODO: 
      WRITE YOUR CODE FOR PROCESSING INCOMING SMS MESSAGES HERE */

   // Sending a reply SMS. If you don't want to send a reply, 
   // just comment all the next lines out.
   context.Response.ContentType = "text/plain";

   // Add an X-SMS-To header to set the recipients of the reply. 
   // If not set, the reply is sent to the sender of the 
   // original SMS message.
   // context.Response.AddHeader("X-SMS-To", 
   //      "+97771234567 +15550987654");

   // Write the text of the reply SMS message 
   // to the HTTP response output stream.
   context.Response.Write("Reply SMS message");
 }

 public bool IsReusable {
   get {
      return false;
   }
 }
}

我唯一想要的是如何在.ashx文件上绑定数据,循环遍历它,检查它是真还是假,然后回复。

1 个答案:

答案 0 :(得分:0)

没关系。我找到了解决方案

SqlConnection con1 = new SqlConnection();
        con1.ConnectionString = ConfigurationManager.ConnectionStrings["NDERMJConnectionString"].ToString();
        string sql1 = "select Count(*) from tablename where name= @name";
        SqlCommand cmd1 = new SqlCommand(sql1, con1);
        cmd1.Parameters.AddWithValue("@name", sms["text"].ToString());
        con1.Open();
        int result = (int)cmd1.ExecuteScalar();
        if (result > 0)
        {
            context.Response.Write("You are the winner");
        }
        else
        {
            context.Response.Write("I'm sorry. Try Again.");
        }