控制器是维护MVC的灵魂所在,当我们创建一个MVC程序时,vs会自动创建一个Controllers的文件夹,如果你这时候右击它,并移动到Add上,便会看见Controller,点击它,并取名为HomeController,确定之后你会看见这样的页面。
public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } }
是的,一个继承了Controller类的对象,并且里面有一个公共方法,这个方法返回一个ActionResult类。我们选中ActionResult,点击F12,也就是查看定义。可以看到这个类的定义。
// Summary: // Represents the result of an action method. public abstract class ActionResult { // Summary: // Initializes a new instance of the System.Web.Mvc.ActionResult class. protected ActionResult(); // Summary: // Enables processing of the result of an action method by a custom type that // inherits from the System.Web.Mvc.ActionResult class. // // Parameters: // context: // The context in which the result is executed. The context information includes // the controller, HTTP content, request context, and route data. public abstract void ExecuteResult(ControllerContext context); }
这货是一个抽象方法,本身并不能被实例化。也就是说,我们必须返回继承他的子类。这时候我们可以看到自动生成的这个方法返回了一个View()。OK,我们看一下这个View()的定义
// // Summary: // Creates a System.Web.Mvc.ViewResult object that renders a view to the response. // // Returns: // The System.Web.Mvc.Controller.View() result that renders a view to the response. protected internal ViewResult View();
这个View方法返回一个ViewResult类,好吧,我们继续探究这个ViewResult类,我们经过两次F12后,终于看到了尽头,这个类果然是继承了ActionResult的。
// Summary: // Represents a class that is used to render a view by using an System.Web.Mvc.IView // instance that is returned by an System.Web.Mvc.IViewEngine object. public class ViewResult:ViewResultBase { // Summary: // Initializes a new instance of the System.Web.Mvc.ViewResult class. public ViewResult(); // Summary: // Gets the name of the master view (such as a master page or template) to use // when the view is rendered. // // Returns: // The name of the master view. public string MasterName { get; set; } // Summary: // Searches the registered view engines and returns the object that is used // to render the view. // // Parameters: // context: // The controller context. // // Returns: // The object that is used to render the view. // // Exceptions: // System.InvalidOperationException: // An error occurred while the method was searching for the view. protected override ViewEngineResult FindView(ControllerContext context); } }
// Summary: // Represents a base class that is used to provide the model to the view and // then render the view to the response. public abstract class ViewResultBase:ActionResult { // Summary: // Initializes a new instance of the System.Web.Mvc.ViewResultBase class. protected ViewResultBase(); // Summary: // Gets the view data model. // // Returns: // The view data model. public object Model { get; } // // Summary: // Gets or sets the System.Web.Mvc.TempDataDictionary object for this result. // // Returns: // The temporary data. public TempDataDictionary TempData { get; set; } // // Summary: // Gets or sets the System.Web.Mvc.IView object that is rendered to the response. // // Returns: // The view. public IView View { get; set; } // // Summary: // Gets the view bag. // // Returns: // The view bag. [Dynamic] public dynamic ViewBag { get; } // // Summary: // Gets or sets the view data System.Web.Mvc.ViewDataDictionary object for this // result. // // Returns: // The view data. public ViewDataDictionary ViewData { get; set; } // // Summary: // Gets or sets the collection of view engines that are associated with this // result. // // Returns: // The collection of view engines. public ViewEngineCollection ViewEngineCollection { get; set; } // // Summary: // Gets or sets the name of the view to render. // // Returns: // The name of the view. public string ViewName { get; set; } // Summary: // When called by the action invoker, renders the view to the response. // // Parameters: // context: // The context that the result is executed in. // // Exceptions: // System.ArgumentNullException: // The context parameter is null. public override void ExecuteResult(ControllerContext context); // // Summary: // Returns the System.Web.Mvc.ViewEngineResult object that is used to render // the view. // // Parameters: // context: // The context. // // Returns: // The view engine. protected abstract ViewEngineResult FindView(ControllerContext context); }
并且在途中还看到了一些奇奇怪怪的属性和方法,欧,饶了我吧,这么多东西,怎么学啊!!!!!!!!额。。。现在我告诉你一件听了你可能会生气的事情,就是我们看了这么多东西,其实,这些东西并没有什么卵用(尼玛!!坑爹啊!(╯°口°)╯(┴—┴
好吧,是暂时没什么卵用,不代表以后没用。( ̄▽ ̄)
我们再回到HomeController类中,在Index方法下右击Add View,直接确定,什么都不用看。你会来到一个cshtml页面。这特么又是什么鬼啊??在这里,我要很高兴地告诉你,你来到了第二曲的地方-视图(就是MVC中的View啦),下一曲开始!
----来自《吹响!上低音号》
时间: 2024-10-13 22:28:55