2014-04-05 15 views
0

我只是想在ASP.Net C#中使用Web請求對象來提交表單。這裏我的問題是提前檢查是否有外部應用程序提出的新請求。在asp.net 3.5中的Request.Params和Request.Forms中保留鍵的數量有多少?

這裏是我的控制檯應用程序:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.IO; 
using System.Collections; 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create web request object 
      WebRequest objWebRequest; 

      // Set url properties 
      string url = "http://localhost:2055/EasyWeb/Admin/Post_History.aspx"; 
      objWebRequest = WebRequest.Create(url); 
      objWebRequest.Method = "POST"; 

      // add sample form data 
      ArrayList queryList = new ArrayList(); 
      queryList.Add(string.Format("{0}={1}", "title", "From Admin to All Users")); 
      queryList.Add(string.Format("{0}={1}", "desc", "hi all users")); 
      queryList.Add(string.Format("{0}={1}", "category", "Test")); 
      queryList.Add(string.Format("{0}={1}", "touser", null)); 
      queryList.Add(string.Format("{0}={1}", "status", null)); 
      queryList.Add(string.Format("{0}={1}", "group", null)); 
      queryList.Add(string.Format("{0}={1}", "isfile", "False")); 
      queryList.Add(string.Format("{0}={1}", "sentdatetime", DateTime.Now.ToString())); 
      // Set the encoding type 
      objWebRequest.ContentType = "application/x-www-form-urlencoded"; 
      string Parameters = String.Join("&", (String[])queryList.ToArray(typeof(string))); 
      objWebRequest.ContentLength = Parameters.Length; 

      // Write stream 
      StreamWriter sw = new StreamWriter(objWebRequest.GetRequestStream()); 
      sw.Write(Parameters); 
      sw.Close(); 

      //we get back the response after submission 
      HttpWebResponse objHttpWebResponse; 
      objHttpWebResponse = (HttpWebResponse)objWebRequest.GetResponse(); 
      StreamReader sr = new StreamReader(objHttpWebResponse.GetResponseStream()); 
     } 
    } 
} 

,這裏是我的web應用程序的頁面加載方法。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Request.Params.Keys.Count >54) // this line getting inconsistent no of value 
     { 
      using (DataClassesDataContext db = new DataClassesDataContext()) 
      { 
       Session["Username"] = db.Users.Where(u => u.type_id.Equals("1")).Select(u => u.Username).FirstOrDefault(); 
       string title = null, desc = null, category = null, touser = null, status = null, group = null, isfile = null, sentdatetime = null; 
       foreach (string strName in Request.Params) 
       { 
        string strValue = Request.Form[strName]; 
        switch (strName) 
        { 
         case "title": 
          title = strValue; 
          break; 
         case "desc": 
          desc = strValue; 
          break; 
         case "category": 
          category = strValue; 
          break; 
         case "touser": 
          touser = strValue; 
          break; 
         case "status": 
          status = strValue; 
          break; 
         case "group": 
          group = strValue; 
          break; 
         case "isfile": 
          isfile = strValue; 
          break; 
         case "sentdatetime": 
          sentdatetime = strValue; 
          break; 
        } 
       } 
       int category_id = db.Categories.Where(c => c.Category_name.Equals(category)).Select(c => c.Id).FirstOrDefault(); 
       int user_id = db.Users.Where(u => u.type_id.Equals("1")).Select(u => u.Id).FirstOrDefault(); 
       System.Nullable<int> touser_id = null; 
       System.Nullable<int> status_id = null; 
       System.Nullable<int> group_id = null; 
       System.Nullable<DateTime> sent_datetime = null; 
       if (!string.IsNullOrEmpty(touser)) 
       { 
        touser_id = db.Users.Where(u => (u.First_name + ' ' + u.Last_name).Equals(touser)).Select(u => u.Id).FirstOrDefault(); 
       } 
       if (!string.IsNullOrEmpty(status)) 
       { 
        status_id = db.Status.Where(s => s.status_name.Equals(status)).Select(s => s.Id).FirstOrDefault(); 
       } 
       if (!string.IsNullOrEmpty(group)) 
       { 
        group_id = db.Groups.Where(g => g.Group_name.Equals(group)).Select(g => g.Id).FirstOrDefault(); 
       } 
       bool is_file = Convert.ToBoolean(isfile); 
       if (!string.IsNullOrEmpty(sentdatetime)) 
       { 
        sent_datetime = DateTime.Parse(sentdatetime); 
       } 
       Post myPost = new Post(); 
       myPost.Title = title; 
       myPost.Category_id = category_id; 
       myPost.Description = desc; 
       myPost.User_id = user_id; 
       myPost.ToUser_id = touser_id; 
       myPost.status_id = status_id; 
       myPost.group_id = group_id; 
       myPost.IsFileAttached = is_file; 
       myPost.Sent_Datetime = sent_datetime; 
       db.Posts.InsertOnSubmit(myPost); 
       db.SubmitChanges(); 
      } 
     } 
     if (!Page.IsPostBack) 
     { 
      var query = Helper.GetProfile().ToList(); 
      foreach (var item in query) 
      { 
       GV_ViewPost.PageSize = item.Page_Size; 
      } 
      Panel_AddNew.Visible = false; 
      Panel_View.Visible = false; 
      Session["CommandName"] = "Inbox"; 
      Session["ColumnName"] = null; 
      Session["SearchtText"] = null; 
      this.FillGrid(Session["CommandName"].ToString(), (String)Session["ColumnName"] ?? null, (String)Session["SearchtText"] ?? null); 
      Bind_DDL_Column_List(); 
      Bind_DDL_Category_List(); 
      Bind_Users_List(); 
      Bind_DDL_Group(); 
      Bind_DDL_Status(); 
     } 
     this.GetData(); 
    } 

這裏是這一行if (Request.Params.Keys.Count >54)沒有修復保留(默認)鍵的計數no。

通過使用這種技術,我必須檢查第一個新的密鑰找到與否。

有沒有人幫助我。

回答

1

如果你想從值Request.Params分配一些變量,只要做到這一點:

string title = Request.Form["title"]; 
string desc = Request.Form["desc"]; 
... 

你並不需要檢查,如果該參數退出。如果不是,那麼變量將爲空。

編輯:空檢查

if(string.IsNullOrEmpty(title) || string.IsNullOrEmpty(desc) || ...) 
{ 
    //Error: throw exception or: Response.Write("Please fill all needed parameters.") 
    return; 
} 
+0

如果變量空ŧ母雞我有錯誤不能插入null值到表值,因爲我寫入提交語句插入值到數據庫。 –

+0

然後你可以檢查null並拒絕請求(發送錯誤作爲響應)。 – Mahmoodvcs

+0

你沒有我從Response.Params的默認密鑰的想法 –

1

只需使用

If Context.Request.QueryString.AllKeys.Contains("keyvalue") Then 
     If String.IsNullOrEmpty(Request.QueryString("keyvalue").ToString) Or String.IsNullOrWhiteSpace(Request.QueryString("keyvalue")) Then 

     End If 
     'or if need 
     If IsNumeric(Request.QueryString("keyvalue")) Then 

     End If 
    End If 

這一切

轉換在C#

if (Context.Request.QueryString.AllKeys.Contains("keyvalue")) { 

if (string.IsNullOrEmpty(Request.QueryString("keyvalue").ToString) | string.IsNullOrWhiteSpace(Request.QueryString("keyvalue"))) { 
} 
//'or if need 

if (Information.IsNumeric(Request.QueryString("keyvalue"))) { 
} 
} 
+0

可以ü只是轉換爲C# –

+0

更新與C#版本 – makemoney2010