简洁的MVC思想框架——Nancy(Post操作与外部引用css和JS)
2024-08-31 13:01:22
之前介绍了关于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类似,无多大变动。
最新文章
- GO语言练习:网络编程 ICMP 示例
- C#报错:创建调试信息文件 ……obj\Debug\model.pdb: 拒绝访问
- 每用户订阅上的所有人SID 不存在
- python爬虫之Scrapy 使用代理配置
- 最近工作用到的sql脚本
- href 里面 链接前面加/与不加的区别?(绝对路径与相对路径)
- IS-A 和 HAS-A
- Swift 了解(1)
- HTML <;hr>;标签
- Wo Wei Shen Me Hui Zai cnblogs Xie Bo Ke
- Cognos 11.0快速开发指南 Ⅰ
- 项目实战2—LVS负载均衡
- 39.QT-Qtxlsx库使用
- Docker设置http代理
- [Python设计模式] 第18章 游戏角色备份——备忘录模式
- .NET BackgroundWorker的一般使用方式
- editable : false与 readonly 的区别
- *单链表[递归&;不带头结点]
- win10系统安装web3js的正确方法
- ConcurrentHashMap 产生NullPointerException