zoukankan      html  css  js  c++  java
  • ASP.NET MVC #02, ASP.NET MVC Overview

    坚持第二天!

    今天我学到了下面六条,感觉很不错:

    1). MVC 是将一个Application分成三大部分:Model, ViewController

    2). MVC的URL 并不是与一个真实存在的.aspx文件相对应,是通过Route进行Route的

    3). Routing, 是将Incoming request route到Controllers actions

    4). Controller,  A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). The URL /Home/Index invokes the HomeController.Index() method and the URL /Home/About invokes the HomeController.About() method.

    5). View, A view should contain only logic related to generating the user interface

    6). Model, An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic

    http://www.asp.net/learn/mvc/tutorial-01-cs.aspx

    The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.

    The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications.

    The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication.

    The MVC framework is defined in the System.Web.Mvc namespace and is a fundamental, supported part of the System.Web namespace.

    http://www.asp.net/learn/mvc/tutorial-02-cs.aspx

    A URL Does Not Equal a Page

    When you build a traditional ASP.NET Web Forms application or an Active Server Pages application, there is a one-to-one correspondence between a URL and a page. If you request a page named SomePage.aspx from the server, then there had better be a page on disk named SomePage.aspx. If the SomePage.aspx file does not exist, you get an ugly 404 – Page Not Found error.

    When building an ASP.NET MVC application, in contrast, there is no correspondence between the URL that you type into your browser’s address bar and the files that you find in your application. In an ASP.NET MVC application, a URL corresponds to a controller action instead of a page on disk.

    In a traditional ASP.NET or ASP application, browser requests are mapped to pages. In an ASP.NET MVC application, in contrast, browser requests are mapped to controller actions. An ASP.NET Web Forms application is content-centric. An ASP.NET MVC application, in contrast, is application logic centric.

    Understanding ASP.NET Routing

    A browser request gets mapped to a controller action through a feature of the ASP.NET framework called ASP.NET Routing. ASP.NET Routing is used by the ASP.NET MVC framework to route incoming requests to controller actions.

    ASP.NET Routing uses a route table to handle incoming requests. This route table is created when your web application first starts. The route table is setup in the Global.asax file. The default MVC Global.asax file is contained in Listing 1.

    Listing 1 – Global.asax

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    namespace MvcApplication1
    {
    // 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
    );

    }

    protected void Application_Start()
    {
    RegisterRoutes(RouteTable.Routes);
    }
    }
    }

    When an ASP.NET application first starts, the Application_Start() method is called. In Listing 1, this method calls the RegisterRoutes() method and the RegisterRoutes() method creates the default route table.

    The default route table consists of one route. This default route breaks all incoming requests into three segments (a URL segment is anything between forward slashes). The first segment is mapped to a controller name, the second segment is mapped to an action name, and the final segment is mapped to a parameter passed to the action named Id.

    For example, consider the following URL:

    /Product/Details/3

    This URL is parsed into three parameters like this:

    Controller = Product

    Action = Details

    Id = 3

    The Default route defined in the Global.asax file includes default values for all three parameters. The default Controller is Home, the default Action is Index, and the default Id is an empty string. With these defaults in mind, consider how the following URL is parsed:

    /Employee

    This URL is parsed into three parameters like this:

    Controller = Employee

    Action = Index

    Id = ��

    Finally, if you open an ASP.NET MVC Application without supplying any URL (for example, http://localhost) then the URL is parsed like this:

    Controller = Home

    Action = Index

    Id = ��

    The request is routed to the Index() action on the HomeController class.

    Understanding Controllers

    A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

    A controller is just a class (for example, a Visual Basic or C# class). The sample ASP.NET MVC application includes a controller named HomeController.cs located in the Controllers folder. The content of the HomeController.cs file is reproduced in Listing 2.

    Listing 2 – HomeController.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace MvcApplication1.Controllers
    {
    [HandleError]
    public class HomeController : Controller
    {
    public ActionResult Index()
    {
    ViewData[
    "Title"] = "Home Page";
    ViewData[
    "Message"] = "Welcome to ASP.NET MVC!";

    return View();
    }

    public ActionResult About()
    {
    ViewData[
    "Title"] = "About Page";

    return View();
    }
    }
    }

    Notice that the HomeController has two methods named Index() and About(). These two methods correspond to the two actions exposed by the controller. The URL /Home/Index invokes the HomeController.Index() method and the URL /Home/About invokes the HomeController.About() method.

    Any public method in a controller is exposed as a controller action. You need to be careful about this. This means that any public method contained in a controller can be invoked by anyone with access to the Internet by entering the right URL into a browser.

     

    Understanding Views

    The two controller actions exposed by the HomeController class, Index() and About(), both return a view. A view contains the HTML markup and content that is sent to the browser. A view is the equivalent of a page when working with an ASP.NET MVC application.

    You must create your views in the right location. The HomeController.Index() action returns a view located at the following path:

    \Views\Home\Index.aspx

    The HomeController.About() action returns a view located at the following path:

    \Views\Home\About.aspx

    In general, if you want to return a view for a controller action, then you need to create a subfolder in the Views folder with the same name as your controller. Within the subfolder, you must create an .aspx file with the same name as the controller action.

    The file in Listing 3 contains the About.aspx view.

    Listing 3 – About.aspx

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

    <asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>About</h2>
    <p>
    Put content here.
    </p>
    </asp:Content>

    If you ignore the first line in Listing 3, most of the rest of the view consists of standard HTML. You can modify the contents of the view by entering any HTML that you want here.

    A view is very similar to a page in Active Server Pages or ASP.NET Web Forms. A view can contain HTML content and scripts. You can write the scripts in your favorite .NET programming language (for example, C# or Visual Basic .NET). You use scripts to display dynamic content such as database data.

    Understanding Models

    We have discussed controllers and we have discussed views. The last topic that we need to discuss is models. What is an MVC model?

    An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.

    A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.

    In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

  • 相关阅读:
    矩阵解压,网络流UESTC-1962天才钱vs学霸周2
    最小生成树唯一性判断-UESTC1959天才钱vs学霸周
    度及拓扑图的使用-UESTC1958学霸周选课
    CodeForces1000A-Light It Up
    CodeForces1000A- Codehorses T-shirts
    CoderForces999F-Cards and Joy
    CoderForces999E-Reachability from the Capital
    CoderForces999D-Equalize the Remainders
    CoderForces999C-Alphabetic Removals
    CoderForces999B- Reversing Encryption
  • 原文地址:https://www.cnblogs.com/holly/p/1661311.html
Copyright © 2011-2022 走看看