zoukankan      html  css  js  c++  java
  • ASP.NET MVC 入门4、Controller与Action

    1. Controller类继承自ControllerBase类, ControllerBase类实现了IController接口.
    2. ControllerBase类实现了Exceute方法, 当URL路由匹配到Controller后,就会执行Excecute方法进行Controller的处理.
    3. ControllerBase类还定义了抽象的ExcecuteCore方法,当Execute执行完毕后就会执行ExecuteCore方法.
    4. ControllerBase类还定义了三个核心的属性 TempData, ViewData.
    5. Controller继承了ControllerBase外, 还实现了一系列的Filter接口.
    6. asp.net mvc应用程序URL都是映射到Controller中的某个Action中,由Action处理逻辑并返回View.
    7. Controller中的Public方法都被当做是Action方法, Action方法通常会返回ActionResult结果.
    8. 默认情况Action方法的名称就是这个Action的Action名, Action名就是Route中匹配Action方法的URL部分. 例如:Home/Index     Index就是Action名.
    9. Action方法默认匹配同名的Action, 但也可以自己定义,通过ActionNameAttribute使Action方法匹配指定名称的Action.
    10. Action方法还可以通过AcceptVerbsAttribute让其匹配Action指定的HttpMethod.(见下面代码段)
    11. 如果要为一个Public方法设置为不是Acion方法,就需要为这个Public方法指定NonAction的Attribute.
    12. Aciton方法中的参数必须和Route中指定的参数名称相同, 当访问URL时, URL参数部分值会自动传递给Action方法的参数.
    13. Controller的逻辑处理中可能会需要用到通用的部分,比如为用户打印错误信息操作, 那么我们可以自己通过继承Controller,实现自己的基类.

      Action方法返回ActionResult类型的结果。ASP.NET MVC为我们提供了几种ActionResult的实现,如下:

      • ViewResult. 呈现视图页给客户端。由View 方法返回.

      • RedirectToRouteResult. 重定向到另外一个Route。由RedirectToActionRedirectToRoute 方法返回.

      • RedirectResult. 重定向到另外一个URL。由 Redirect 方法返回.

      • ContentResult. 返回普通的内容。例如一段字符串。由 Content 方法返回.

      • JsonResult. 返回JSON结果。由 Json 方法返回.

      • EmptyResult. 如果Action必须返回空值,可以返回这个结果。Controller中没有实现的方法,可以return new EmptyResult();.

      当然我们也可以自定一个我们的ActionResult返回给客户端,例如一个RssResult。

    14.Controller的逻辑处理中可能会需要用到通用的部分,比如为用户打印错误信息操作, 那么我们可以自己通过继承Controller,实现自己的基类.

    [AcceptVerbs("GET")]
    public ActionResult Setting()
    {
        throw new NotImplementedException();
    } 
    
    [ActionName("Setting"), AcceptVerbs("POST")]
    public ActionResult SaveSetting()
    {
        throw new NotImplementedException();
    }
  • 相关阅读:
    关于gtk的GCond
    位运算符及其应用
    登陆新浪微博&批量下载收藏内容[Python脚本实现]
    海量数据处理算法—Bloom Filter
    海量数据处理算法—BitMap
    VB.NET机房收费系统——组合查询
    非官方的gstreamer学习资料及概念摘要
    [Python入门及进阶笔记00]写在前面(目录/书籍/学习路线/其他)
    [JAVA][Eclipse]JVM terminated. Exit code=13
    介绍一个android开源文件选择对话框:androidfiledialog
  • 原文地址:https://www.cnblogs.com/ybtools/p/3793306.html
Copyright © 2011-2022 走看看