先来看下HTTP请求,很多学.net的人都不太了解,就知道拖控件不解释:
下面在看下URL各个参数:
看图我不解释了。
在下面看下post与get的简单区别:
在FORM提交的时候,如果不指定Method,则默认为GET请求(.net默认是POST),Form中提交的数据将会附加在url之后,以?分开与url分开。字母数字字符原样发送,但空格转换为“+”号,其它符号转换为%XX,其中XX为该符号以16进制表示的ASCII(或ISO Latin-1)值。GET请求请提交的数据放置在HTTP请求协议头中,而POST提交的数据则放在实体数据中;GET方式提交的数据最多只能有2048字节,而POST则没有此限制。POST传递的参数在doc里,也就http协议所传递的文本,接受时再解析参数部分。获得参数。一般用POST比较好。POST提交数据是隐式的,GET是通过在url里面传递的,用来传递一些不需要保密的数据,GET是通过在URL里传递参数,POST不是。
说明:关于“POST与GET的差异”查考了网上前辈的资料,由于找不出源头,到处都是转帖,这里就不贴出相关网址了,baidu或Google下就知道了
如果你只知道post与get这2种请求方式,那么下面就请颤抖吧~~~随着Ajax XMLHttpRequest 和 REST风格应用的深入。HTTP还支持下面的请求方式:
初体验(二)就是做个留言板小程序,没用数据库:
我直接上代码:
Comment.cs类
using System;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// Summary description for ClassName
/// </summary>
public class Comment
{
public Comment()
{
//
// TODO: Add constructor logic here
//
}
public Guid Id { set; get; }
public string Username { set; get; }
public string Content { set; get; }
public DateTime CreatedTime { set; get; }
public Guid? ParentID { set; get; }
}
Index.cshtml代码:
@{
if (IsPost)
{
var content = Request.Form["content"];
var username = Request.Form["username"];
var comment = new Comment
{
Content = content,
Username = username,
CreatedTime = DateTime.Now.AddDays(-2),
Id = Guid.NewGuid()
};
if (Request.Form["parentid"] != "")
{
comment.ParentID = new Guid(Request.Form["parentid"]);
}
var list = Context.Application["list"] as List<Comment>;
if (list == null)
{
list = new List<Comment>();
Context.Application["list"] = list;
}
list.Add(comment);
Response.Redirect("Index.cshtml");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<ul>
@{
var allList = Context.Application["list"] as List<Comment> ?? new List<Comment>();
}
@foreach (var item in allList.Where(c=>!c.ParentID.HasValue))
{
<li>内容:@item.Content<br />
发布人:@item.Username @@ @item.CreatedTime.ToString("MM:dd HH:mm:ss")<br />
<a href="?id=@item.Id">回复</a>
<ul>
@foreach (var subitem in allList.Where(c => c.ParentID == item.Id))
{
<li>@subitem.Content|@subitem.Username</li>
}
</ul>
</li>
}
</ul>
<form action="" method="post">
<input type="hidden" name="parentid" value="@Request.QueryString["id"]" />
<label>
内容</label>
<textarea name="content"></textarea><br />
<label>
用户名</label>
<input type="text" name="username" />
<input type="submit" value="提交" />
</form>
</body>
</html>