ASP. net mvc mavericks Journey 2: experience the first MVC program, asp. netmvc

Source: Internet
Author: User
Tags configuration settings

ASP. net mvc mavericks Journey 2: experience the first MVC program, asp. netmvc

After learning about MVC, I will use a very simple message board program to briefly understand the MVC website development process and outline MVC development. The First project won't mention too many database-related technologies. Therefore, we will use the Framework Code First development technology for data access. The core is to experience the MVC development process.

2.1Create a project using the ASP. NET MVC4 Project template

Enable Visual Studio2012 and select "file"> "project" menu command

In the open "new project" dialog box, expand web → "ASP. NET MVC4 Web application" and change the name to "MvcGuestbook ":

Click OK. The project template selection page is displayed, asking which project template you want to use. Select "Internet application", and click "OK ,:

This MVC project has been fully created. Next, "debug" → "start debugging" (or press F5) to run the website.

This website has very basic functions, including simple Page and membership mechanisms. All these pages use the master Page (Layout Page) and ASP. NET built-in Membership function, which can be used for Member registration, login, and cancellation. :

ASP. the NETMVC4 Project template has a built-in member mechanism. After the webpage runs the page related to this member mechanism for the first time, it will automatically create the default database document (*. mdf ,*. ldf), the file naming rule will be "asp.net-project name-date and time. mdf ".

After the project is created, several standard directory structures and important documents are automatically created ,:

 

MVC Project Overview:

  

Folder or file

Description

Note

/App_Data

Private Data Storage: the storage (data) library for XML files, SQLite, SQL Server database files, or other basic files

IIS does not provide content services for this folder (Web requests cannot be sent)

/App_Start

Configuration settings that include the core of the project: routing, filters, and other content packages (script style bundle, etc)

 

/Bin

Stores the compiled assembly of the MVC application, and does not reference the assembly in the GAC (Global Assembly Cache ).

IIS does not provide content services for this folder (Web requests cannot be sent ). "Show All Files" can be seen in the project. Compiled binary files should not be placed in source control.

/Content

Place static content: CSS files, images, etc.

Conventions, not required.

/Controllers

Placement Controller

Convention: the Controller class can be placed anywhere or defined in an independent project.

/Models

Place view models or domain model classes (simple projects ).

Convention. Generally, a dedicated project definition domain model is used. This folder only contains the view model.

 

/Scripts

Stores JS libraries, JQuery and several popular libraries Conventions, changeable
/Views SaveViewAndDivision View, Usually named by the associated Controller /Views/Web. config prevents IIS from serving the contents of this directory.Action MethodRendering
/Views/Shared SaveLayout,Shared view (non-dedicated)  
/Views/Web. config It is not an application configuration file. The full view can work with ASP. NET to prevent the configuration required by IIS to call the view. The namespace of the view is imported by default.  
/Global. asax Global ASP. NET application class. The background code class (Global. asax. cs) is usedRegister route ConfigurationCreate the code that runs when the program is initialized, shut down, or an unknown exception occurs. Same as Global. asax in web Form
/Web. config Application configuration file. Same as web. config in Web Form

 

2.2MVC conventions

1. Recommended user Conventions: Scripts folder, etc.

2. "convention is better than configuration": you do not need to explicitly configure the association between the Controller and Its view, as long as you follow the naming convention, you can work normally, such:

(1) The Controller class convention must end with "Controller". The category inherits from the Controller base class and must contain several public methods (Action Actions) with the return value of ActionResult );

(2) view conventions. View and branch view are placed in the/Views/<Controller Name> folder (ignore the ending Controller );

(3) Layout conventions. The following underscore (_) is used as the prefix of the file name and placed in the/Views/SHared folder. In addition to the Empty project template, VS creates a Layout named _ Layout. cshtml. By default, the layout is applied to all Views through the/Views/_ ViewStart. cshtml file.

2.3 create a data Model)

In Solution Explorer, select the "Model" directory, right-click the directory, and choose "add"> "class" from the shortcut menu ,:

The Code is as follows:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5  6 namespace MvcGuestbook.Models 7 { 8     public class Guestbook 9     {10         public int Id { get; set; }11         public string Name { get; set; }12         public string Email { get; set; }13         public string Content { get; set; }14     }15 16 }


Generate a solution and ensure that there are no problems.

2.4 create controllers, actions, and views

In the solution Resource Manager window, select the Controller directory, right-click, select "add"> "Controller" from the shortcut menu, and name it "GuestbookController"

You can set four options in the base frame option area. Here, we select the "MVC controller containing read/write operations and views (using Entity Framework)" option under the template, in the model class, select the new Guestbook model category.

In the "basic options" in the "add controller" dialog box, there is a "data context class" option, because we have not created a "data context class ", however, we can use this new project to automatically create the data. Therefore, we can select <new data context...> "option, select the default name, and click" add "to add the controller ,:

Code automatically created by GuestbookController:

  1 namespace MvcGuestbook.Controllers  2 {  3     public class GuestbookController : Controller  4     {  5         private MvcGuestbookContext db = new MvcGuestbookContext();  6   7         //  8         // GET: /Guestbook/  9  10         public ActionResult Index() 11         { 12             return View(db.Guestbooks.ToList()); 13         } 14  15         // 16         // GET: /Guestbook/Details/5 17  18         public ActionResult Details(int id = 0) 19         { 20             Guestbook guestbook = db.Guestbooks.Find(id); 21             if (guestbook == null) 22             { 23                 return HttpNotFound(); 24             } 25             return View(guestbook); 26         } 27  28         // 29         // GET: /Guestbook/Create 30  31         public ActionResult Create() 32         { 33             return View(); 34         } 35  36         // 37         // POST: /Guestbook/Create 38  39         [HttpPost] 40         [ValidateAntiForgeryToken] 41         public ActionResult Create(Guestbook guestbook) 42         { 43             if (ModelState.IsValid) 44             { 45                 db.Guestbooks.Add(guestbook); 46                 db.SaveChanges(); 47                 return RedirectToAction("Index"); 48             } 49  50             return View(guestbook); 51         } 52  53         // 54         // GET: /Guestbook/Edit/5 55  56         public ActionResult Edit(int id = 0) 57         { 58             Guestbook guestbook = db.Guestbooks.Find(id); 59             if (guestbook == null) 60             { 61                 return HttpNotFound(); 62             } 63             return View(guestbook); 64         } 65  66         // 67         // POST: /Guestbook/Edit/5 68  69         [HttpPost] 70         [ValidateAntiForgeryToken] 71         public ActionResult Edit(Guestbook guestbook) 72         { 73             if (ModelState.IsValid) 74             { 75                 db.Entry(guestbook).State = EntityState.Modified; 76                 db.SaveChanges(); 77                 return RedirectToAction("Index"); 78             } 79             return View(guestbook); 80         } 81  82         // 83         // GET: /Guestbook/Delete/5 84  85         public ActionResult Delete(int id = 0) 86         { 87             Guestbook guestbook = db.Guestbooks.Find(id); 88             if (guestbook == null) 89             { 90                 return HttpNotFound(); 91             } 92             return View(guestbook); 93         } 94  95         // 96         // POST: /Guestbook/Delete/5 97  98         [HttpPost, ActionName("Delete")] 99         [ValidateAntiForgeryToken]100         public ActionResult DeleteConfirmed(int id)101         {102             Guestbook guestbook = db.Guestbooks.Find(id);103             db.Guestbooks.Remove(guestbook);104             db.SaveChanges();105             return RedirectToAction("Index");106         }107 108         protected override void Dispose(bool disposing)109         {110             db.Dispose();111             base.Dispose(disposing);112         }113     }114 }

In this case, a GuestbookController. cs is added to the Controllers directory. In addition, because the "MVC controller including read/write operations and views (using Entity Framework)" template was selected when we added the template, therefore, in addition to adding a controller, the Controller and all view pages are created once. A data context class (DateContextClass) is added during the process of adding a controller. Therefore, an MvcGuestbookContext. cs file is added to the Model directory ,:

Finally, the project has been completed and the test page is:

Run the website directly, start debugging, open the webpage, Enter http: // localhost: 28039/Guestbook, and press Enter to Enter the page.

For simple addition, deletion, modification, and query:

So far, the first MVC experience website is complete, and there are still many improvements to this message board. Due to limited technical skills, it may not be completely correct and is for reference only.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.