Problems to be paid attention to when using the MVC Framework (2): use the Model and Controller separately in a project design

Source: Internet
Author: User

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);        }    }}
 

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.