公式学習MVC 01

21757 ワード

1、新規プロジェクト
新しいプロジェクトの作成をクリックし、ASPを選択します.NET webアプリケーションは、プロジェクトに名前を付けて「作成」をクリックします.
スクリーンショットは次のとおりです.
HTTPS構成のチェックを外す
オプション選択+mvc
またはMVCを直接選択
 
2、目次構造分析
1) App_Start
フォルダを設定します.
BundleConfig.cs 
パッケージング(css,jsなど)
            // ScriptBundle:     (     ).includes(       )
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

jqを導入して経路を最初の括弧コンテンツとして再規定し、その後、#1括弧コンテンツviewを使用して呼び出すことができます.以下のようにします.
bundlesを通ります.Add新規パッケージ設定
    // _Layout.cshtml
  @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false)

次のセクション:
            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

jqueryですべて導入されます.validate先頭のファイル
対応するコントローラのビューを作成したら、createなどのテンプレートを選択し、[参照スクリプトライブラリ](Reference Script Library)にチェックマークを付けると、ビューファイルに上記のファイルを参照できます.
次のセクション:
            //             Modernizr      。  ,    
            //https://modernizr.com
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js"));

次のセクションでは、スタイルファイルについて説明します.
            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }

同時に2つのスタイルファイルをパッケージしました
テンプレートファイル内で@Styles.Render導入
Webに戻ります.configファイル
system.Webラベル下compilationラベルdebugプロパティ
true:デバッグモードはパッケージ化されず、元のファイルを参照します.
false:パブリッシュモードパッケージ、圧縮ファイル参照
falseに変更すると参照ファイルのパスが異なります
 
 
FilterConfig.csフィルタ:書き込みフィルタルール
RouteConfig.csルーティング構成
2) Content
bootstrap cssスタイルなどのスタイルファイルを保存
3)Contollers
せいぎょそうち
4)fonts
bootstrapフォント(icon)ファイル
5)Models
データ#データ#
6)Scripts
jsなどのスクリプトファイルを保存
7)Views
表示
アドレスバーはコントローラにアクセスし、Modelsデータを介してフィルタリングし、最終的にViewを表示します.
各Controllerには、対応するViewの下にあるフォルダがあります.
_ViewStart.cshtml
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Layoutテンプレート(レイアウトページ)を設定し、Sharedの下にある_Layout
別のレイアウトページへのパスを記入することもできます
テンプレートを使用しない場合は、次のことを宣言します.
@{
    ViewBag.Title = "About";
    Layout = null;
}

Shared 
_Layout.cshtml共有ビューテンプレート、RenderBody()
 
2、Controllerはどのように運行していますか.
namespace MVCStudy.Controllers
{
    public class DemoController : Controller
    {
        // GET: Demo
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CreateStudent(Models.Student stu)
        {
            return View();
        }
    }
}

1)
アドレスから該当する名前のControllerとその下の対応方法を呼び出す
各要求NoはIISテストフレームワークのパイプに入ります.
このパイプはアドレスバーの関連情報を解析し、ControllerとActionを解析します.
その後、パイプラインがControllerをインスタンス化し、メソッドを呼び出します.
 
2)組み込みオブジェクト解析
含む
Requestリクエスト
 
Responseレスポンス
 
セッション
 
Cookieキャッシュ
 
アプリケーションの現在のWebサイト・オブジェクト
 
サーバ・オブジェクト
 
①Request
サーバがクライアントのデータを受信
クエリー文字列の受信(get):
 
        public ActionResult Index()
        {
            return Content(Request.QueryString["name"]);
        }

複数のクエリー文字列
        public ActionResult Index()
        {
            return Content($"{ Request.QueryString["name"]}-{ Request.QueryString["age"]}");
        }

postリクエストデータの取得
htmlページを使用してデータをコミットし、右クリックでブラウザを開くことができます.
DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>title>
head>
<body>
    <form action="/Demo/Index" method="post">
        <input type="password" name="password" value="" />
        <input type="submit"  value="  " />
    form>
body>
html>

Controller内処理:
        public ActionResult Index()
        {
            return Content(Request.Form["password"]);
        }

 
ファイルのアップロード
 
DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>title>
head>
<body>
    <form action="/Demo/Index" method="post" enctype="multipart/form-data">
        <input type="file" name="file" value="" />
        <input type="submit" value="  " />
    form>
body>
html>
        public ActionResult Index()
        {
            // SaveAs        ,       ,     MapPath  
            Request.Files["file"].SaveAs(filename:Request.MapPath("~/uploads/" + Request.Files["file"].FileName));
            return Content("ok");
        }

ブラウザにアドレスを入力しても関連ファイルにアクセスできます
https://localhost:44386/uploads/a.txt
 
② Response
テキストに応答:
        public ActionResult Index()
        {
            Response.Write("hello,mvc");
            return Content("finished");
        }

 
要求ヘッダ情報の表示
    public ActionResult Index()
        {
            //        
            return Content(Request.Headers["token"]);
        }

 
//レスポンスヘッダの設定:
        public ActionResult Index()
        {
            Response.Headers["userId"] = "linda123";
            return Content("ok");
        }

 
③Session
すべての人それぞれのデータストレージ
本質的にはいくつかのキー値ペアです
持続時間は20 min
相互作用がある場合に有効時間を再計算
サーバに格納されている場所
サーバのパフォーマンスに影響を与える
ログイン情報の保存によく使用されます
すべてのページでsessionデータを使用できます
セッションの設定
 1 <html>
 2 <head>
 3     <meta charset="utf-8" />
 4     <title>title>
 5 head>
 6 <body>
 7     <form action="/Demo/Index" method="post">
 8         <input type="type" name="user" value="" />
 9         <input type="submit" name="name" value="  " />
10     form>
11 body>
12 html>
        public ActionResult Index()
        {
            Session["user"] = Request.Form["user"];
       //
return Content(" " + Session["user"]); }

 
セッションを削除する方法(セキュア終了)
 
      public ActionResult Index()
        {
            Session["user"] = Request.Form["user"];
            return Content("     " + Session["user"]);
        }