Many may ask this question. MVC makes division of labor and collaboration possible. However, if all code and pages are in one project, the division of labor is limited. In fact, both Model and Controller can be implemented independently using one or more programs.
1. Model
The Model is mainly responsible for reading and writing data. Currently, we can use the linq to SQL type or Entity Framework model directly.
Strictly speaking, the Model is divided into two parts: one is the definition of the business entity (which needs to be shared with the View) and the other is the operation on the business entity. However, the two methods mentioned above combine them into one.
After completing the Model project separately, you only need to add references to the MVC Web Application. Very simple and natural.
2. Controller
This is a little more complicated. Please follow the following specifications
- The Controller name must comply with specifications, such as BlogController. A Blog is the name of the Controller, while a Controller is the suffix.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web.Mvc;namespace MyControllers{ public class BlogController:Controller { public ActionResult Index() { return View(); } }}
- Add namespace registration in the global. asax file to let the MVC engine know how to find the Controller
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace MvcApplication2{ // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" }, // Parameter defaults new[] {"MyControllers"} ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } }}