之前介绍了关于Nancy配置与Get基础操作,以下来介绍有关Nancy的Post操作。

第一步,设计主界面,以登录界面为例:Login.cshtml

路径为:

设计好页面后,在之前的modules类中设定指向路径和相关post操作,如下图所示:

<body>
<div id="login">
<form action="/Home" method="post" id="form1">
<div id="bigimg">
<img src="data:images/bigimg.jpg" />
</div>
<div id="loginbox">
<div id="title-login">登录</div>
<a id="title-register" href="#">注册账号 ></a>
<div id="forinput">
帐号:<br>
<p>
<input id="user" name="userId" tabindex="1" type="text">
<span id="error_userId"></span>
</p>密码:
<a href="#" style="color: #00A1D6; float: right; *margin-top: -10px; display: block;"> 忘记了密码? </a>
<br>
<p>
<input id="password" type="password" tabindex="2" name="userPwd">
<span id="error_userPwd"></span>
</p>
验证码:
<br>
<p>
<input id="code" name="verifity" tabindex="3" type="text">
<span>
<img id="Verifity" onclick="src = '/CreateCode?'+Math.random()" src="/CreateCode" style="vertical-align: middle;">
</span>
<span id="error_code" style="margin-left: 18px;"></span>
</p>
</div>
<input type="submit" id="submit" value="登 录" onclick="return checkLogin()">
</div>
</form>
</div>
</body>
 #region 登录——表单验证,成功跳转至主页面

             Post["/Home"] = p =>
{
//获取表单
var user = SqlHelperServer.ToDBValue(Request.Form);
//验证用户名是否存在
if (!userService.isUserId(user.userId))
{
return "<script>alert('用户名不存在!');location.href='/'</script>";
}
//验证密码输入是否正确
if (userService.GetUserPwd(user.userId) != user.userPwd)
{
return "<script>alert('密码错误!');location.href='/'</script>";
}
if (user.verifity != Session["Code"].ToString())
{
return "<script>alert('验证码错误!');location.href='/'</script>";
}
ViewBag.UserId = user.userId;
DataTable dt = userService.GetUser();
return View["User/Home.cshtml", dt.Rows];
}; #endregion

其中关于Sqlhelperserver是有关数据库操作公共类,其中ToDBvalue是获取页面传入所有html的表单数据,其中标签必须有“name”属性。

public static object ToDBValue(this object value)
{
return value == null ? DBNull.Value : value;
}

至此,Post操作完成!

其中还涉及到有关引用外部JS和外部css的文件,这根普通的MVC不一样,也经过相当长的研究才摸索出来,最后发现很简单,只要在modules新建一个类,如下:

public class Bootstrapper : DefaultNancyBootstrapper
{ protected override void ConfigureConventions(NancyConventions nancyConventions)
{
base.ConfigureConventions(nancyConventions);
nancyConventions.StaticContentsConventions.Clear();
nancyConventions.StaticContentsConventions.Add
(StaticContentConventionBuilder.AddDirectory("css", "/css"));
nancyConventions.StaticContentsConventions.Add
(StaticContentConventionBuilder.AddDirectory("js", "/script"));
nancyConventions.StaticContentsConventions.Add
(StaticContentConventionBuilder.AddDirectory("images", "/images"));
}
}

在之后还会讲解关于验证码的获取与Session的相关操作,至于其他跟MVC类似,无多大变动。

最新文章

  1. GO语言练习:网络编程 ICMP 示例
  2. C#报错:创建调试信息文件 ……obj\Debug\model.pdb: 拒绝访问
  3. 每用户订阅上的所有人SID 不存在
  4. python爬虫之Scrapy 使用代理配置
  5. 最近工作用到的sql脚本
  6. href 里面 链接前面加/与不加的区别?(绝对路径与相对路径)
  7. IS-A 和 HAS-A
  8. Swift 了解(1)
  9. HTML &lt;hr&gt;标签
  10. Wo Wei Shen Me Hui Zai cnblogs Xie Bo Ke
  11. Cognos 11.0快速开发指南 Ⅰ
  12. 项目实战2—LVS负载均衡
  13. 39.QT-Qtxlsx库使用
  14. Docker设置http代理
  15. [Python设计模式] 第18章 游戏角色备份——备忘录模式
  16. .NET BackgroundWorker的一般使用方式
  17. editable : false与 readonly 的区别
  18. *单链表[递归&amp;不带头结点]
  19. win10系统安装web3js的正确方法
  20. ConcurrentHashMap 产生NullPointerException

热门文章

  1. Android宝宝点点乐游戏源码完整版
  2. Oracle安装后命令行中运行sqlplus / as sysdba出现错误ora-01031:insufficient privileges
  3. day12 字符编码
  4. 路飞学城Python-Day20
  5. 【洛谷4941】War2 状压Dp
  6. bzoj 1814 Fornula 1
  7. NOI 2018 屠龙勇士 (拓展中国剩余定理excrt+拓展欧几里得exgcd)
  8. JSp获取到当前用户的全部session
  9. jquery @keyframes 动态添加
  10. THINKPHP实现搜索分页保留搜索条件