0
我有一些用戶將從外部站點被定向到我的應用程序,在此重定向過程中,用戶的電子郵件地址將通過HTTP POST發送給我。ASP.NET HTTP POST和重定向
我試圖完成的是接收用戶的電子郵件地址,並將用戶發送到索引視圖。
我有以下幾種方法:
[HttpGet]
public ActionResult Index()
{
string email = "";
string[] keys = Request.Form.AllKeys;
for (int i = 0; i < keys.Length; i++)
{
Response.Write(keys[i] + ": " + Request.Form[keys[i]] + "<br>");
System.Diagnostics.Debug.WriteLine(keys[i] + ": " + Request.Form[keys[i]]);
email = Request.Form[keys[i]].ToString();
}
return Index(email);
}
[HttpPost]
public ActionResult Index(string email)
{
return View();
}
這是我模仿外部站點的工作,這是重定向用戶到我的看法與用戶的電子郵件地址的方法。
public ActionResult Httppost()
{
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data["email"] = "[email protected]";
var response = wb.UploadValues("http://localhost:57695/Home/Index", "POST", data);
}
return RedirectToAction("Index");
}
問題是,索引從來沒有收到我的Httppost()方法的電子郵件地址,並且電子郵件總是以「」形式接收。我怎樣才能從Httppost()收到電子郵件到Index()?
謝謝你的解釋。我現在明白了。但是,您提供的解決方案導致我使用URL欄如下所示:http:// localhost:57695 /?email = test%40email.com。我需要避免用戶通過查詢字符串操縱電子郵件。這可以做到嗎? – dexter
實際上,我需要以HTTP Post的方式發送電子郵件,而不是通過方法調用。因此,返回RedirectToAction(「Index」,new {email =「[email protected]」});沒有達到我相信的要求。 – dexter
@UğurDinç:一個帖子必須來自客戶端,而不是服務器。您需要在視圖中發佈到目標網址的表單,而不是重定向。另外請注意,用戶可以像查詢字符串數據一樣容易地操作發佈數據。如果你需要以某種方式保證數據「安全」,那麼你需要保持它在服務器端,也許在會話或類似的東西。 – David