zoukankan      html  css  js  c++  java
  • ASP.NET MVC

    组成部分

    Model, View, Controller

    Razor 语法(.cshtml文件)

       

    @{} 括号中 C# 代码

    括号中写 C# 代码

    @if()

    {

       

    }

    @foreach()

    {

       

    }

       

    @() 里面是 C# 表达式

       

    ~/ 开始为虚拟路径

    Controller View 传递数据的方式

    ViewData ViewBag 传递数据

    1. ViewData["name"] = value; 在 cshtml 中可直接使用 ViewData["name"] 获取
    2. ViewBag.name = value; 在 cshtml 中可使用 ViewBag.name 获取
    3. ViewData 和 ViewBag 共享数据

       

    Model 传递数据

    1. Controller 的某个方法(Action)中返回 return View(model) 传入
    2. 弱类型
      1. 不在 cshtml 中没有声明 @model 模型类的全称
    3. 强类型
      1. 在 cshtml 中没有声明 @model 模型类的全称

       

    请求中携带参数/path?id=1, Controller 中的 Action 如何接受

    1. MethodName(MethodNameModel model)
    2. MethodName(int id)

       

    注意

    1. 默认一般情况下 Controller 中的 Action 是无法重载的, 否则会报错
    2. 但是通过添加 HttpGet, HttpPost 等 Attribute 则可以

    Controller ActionResult

    1. ViewResult
    2. RedirectResult
    3. FileResult
    4. JsonResult
      1. return Json(data) // 默认不允许 GET

       

    View() Redirect() 区别

    1. View() 是同一个请求(和其他的 Action 共享 ViewData 和 ViewBag)
    2. Redirect() 要求客户端发起一个新的请求(ViewData和ViewBag会清空)

    Controller 属性

    1. ViewData
    2. ViewBag
    3. Session
    4. TempData: 取出就销毁

    数据验证

       

    在 model 的属性上添加属性

    1. Required
    2. StringLength(num, MinimumLength=num1)
    3. Range(a, b)
    4. Compare("")
    5. EmailAddress
    6. Phone
    7. RegularExpression

       

    每个 Attribute 中有 ErrorMessage参数

       

    自定义验证

    1. 继承 ValidationAttribute
      1. 重写 IsValid(object value)
    2. 如果希望实现正则表达式, 直接继承 RegularExpression
      1. 构造方法注意使用 base("期望的正则表达式")
      2. 在构造方法中 this.ErrorMessage = ""用来提示错误

    Filters

       

    全局的

    1. IAuthentication
    2. IAction

       

    局部的

    1. 继承 FilterAttribute, 在期望的Action添加属性

       

    FilterContext 对象

    1. filterContext.ActionDescriptor.ControllerDescriptor.ControllerName
    2. filterContext.ActionDescriptor.ActionName
    3. filterContext.HttpContext.Session
    4. filterContext.Result
    5. filterContext.HttpContext.Response(.Redirect()

       

    注册

    在 Global.asax.cs 中使用 GlobalFilters.Filters.Add(new Xxx())

    Linq

    示例代码

    1. from item in items

      select item.value

    2. from item in items

      select new {id=item.Id, Name=item.Name}

    3. from item in items

      orderby item.id descending

      select item

    4. from item in items

      join other in others on item.Id equals other.Id

      select new {itemName=item.name, otherName=other.Name}

       

    MVC 项目三层架构

    UI (MVC 就是 UI 层), BLL (业务逻辑层), DTO(Data Translate Object), DAL(数据访问层)

    可以将 BLL 和 DAL 合并成 Service 层

  • 相关阅读:
    二分查找(通过相对位置判断区间位置)--17--二分--LeetCode33搜索旋转排序数组
    归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对
    22-Java-Hibernate框架(二)
    21-Java-Hibernate框架(一)
    操作系统-5-进程管理(二)
    操作系统-4-进程管理(一)
    操作系统-3-操作系统引论
    操作系统-2-存储管理之LRU页面置换算法(LeetCode146)
    20-Java-正则表达式
    19-Java-核心类库2-包装类、Integer类、String类、StringBuffer类、StringBuilder类
  • 原文地址:https://www.cnblogs.com/megachen/p/12636459.html
Copyright © 2011-2022 走看看