簡潔的MVC思想架構——Nancy(Post操作與外部參考css和JS)

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   for   

之前介紹了關於Nancy配置與Get基礎操作,以下來介紹有關Nancy的Post操作。

第一步,設計主介面,以登入介面為例:Login.cshtml

路徑為:

設計好頁面後,在之前的modules類中設定指向路徑和相關post操作,如所示:

<body>    <div id="login">        <form action="/Home" method="post" id="form1">            <div id="bigimg">                <img src="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>
View Code
 1 #region 登入——表單驗證,成功跳轉至首頁面 2  3             Post["/Home"] = p => 4             { 5                 //擷取表單 6                 var user = SqlHelperServer.ToDBValue(Request.Form); 7                 //驗證使用者名稱是否存在 8                 if (!userService.isUserId(user.userId)) 9                 {10                     return "<script>alert(‘使用者名稱不存在!‘);location.href=‘/‘</script>";11                 }12                 //驗證密碼輸入是否正確13                 if (userService.GetUserPwd(user.userId) != user.userPwd)14                 {15                     return "<script>alert(‘密碼錯誤!‘);location.href=‘/‘</script>";16                 }17                 if (user.verifity != Session["Code"].ToString())18                 {19                     return "<script>alert(‘驗證碼錯誤!‘);location.href=‘/‘</script>";20                 }21                 ViewBag.UserId = user.userId;22                 DataTable dt = userService.GetUser();23                 return View["User/Home.cshtml", dt.Rows];24             };25 26             #endregion
View Code

其中關於Sqlhelperserver是有關資料庫操作公用類,其中ToDBvalue是擷取頁面傳入所有html的表單資料,其中標籤必須有“name”屬性。

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

至此,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"));        }}
View Code

在之後還會講解關於驗證碼的擷取與Session的相關操作,至於其他跟MVC類似,無多大變動。

簡潔的MVC思想架構——Nancy(Post操作與外部參考css和JS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.