LoginSignup
2
3

More than 3 years have passed since last update.

Visual Studio でASP.NET MVCを使ってみた

Last updated at Posted at 2019-08-30

image.png

フレームワークはサーバーに合わせましょう!
image.png

image.png

接続先の変更

サーバーエクスプローラーから接続変更

[サーバーエクスプローラー]-[データ接続]-[(右クリック)接続の変更]

Web.configの変更

[接続先のプロパティ]-[接続文字列] をコピー
image.png

[Web.config]-[connectionStrings]を書き換える
image.png

コントローラー追加

image.png

image.png

コントローラーとビュー(フォルダ)が作成される

View(xxx.cshtml)を追加する

image.png

ビューに対応するコントローラーの記述

Controllers/BooksController.cs
  // GET: Books/Create
        public ActionResult Create()
        {
            return View();
        }

ルーティング

ルーティングはApp_Start/RouteConfig.csによって制御されているが、
基本的な使い方においては特に触る必要なし

image.png

App_Start/RouteConfig.cs
  public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

http://localhost:29400/controller(books)/action(index)/id(オプション)

スタートアップの設定

スタートページにしたViewの[cshtlmlファイル]-[右クリック]-[スタートページに設定]

image.png

[プロジェクト名]-[プロパティ]-[Web]-[(開始動作)ページを指定する]
※デフォルトでは間違っていたので修正した
image.png

起動させてみる

開始ボタンをクリック

image.png

表示させたいURLを入力

http://ローカルアドレス/階層/ページ

http://localhost:29400/Books/index

テーブルが作成される

この段階ではBooksテーブルは作成されていないが、「Create New」をクリックすることで作成される
image.png

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3