zoukankan      html  css  js  c++  java
  • .net MVC简介、项目中每个文件夹的功能

    MVC是微软2009对外公布的第一个开源的表示层框架,这是微软的第一个开源项目

    M:viewmodel  V:视图  c:控制器

    App_Data:一个比较特殊的文件夹,把文件放到这个文件夹,通过地址访问是访问不到的,ASP.NET会帮我们屏蔽掉。通常放一些数据库文件、xml配置文件以防止数据泄露

    Content:一般放CSS样式、样式图片

    Controllers:放控制器文件

    Models:放ViewsModels

    Scripts:放js、JQ

    Views:放视图的文件夹

    Global.asax:全局应用程序

    RouteConfig:路由的配置

    伪静态:

                routes.MapRoute(
                    "Default2",
                    "{controller}-{action}.html",//记得在IIS添加映射 处理成映射--添加托管处理程序  *.html/System.Web.UI.PageHandlerFactory/name
                    new { controller = "Home", action = "Index"}
                );

    传值:通过viewData传值

    控制器:

            /// <summary>
            /// 得到实体
            /// </summary>
            /// <param name="Account">账号</param>
            /// <param name="password">密码</param>
            /// <returns></returns>
            public MTManagementTools.Model.ToolMember GetModel(string Account, string password)
            {
    
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select  top 1 ID,Account,PassWord,MenuIDs,Name,Sex,Phone,Address,QQ,IsDel from MtToolMember ");
                strSql.Append(" where Account=@Account and PassWord=@PassWord and IsDel=0");
                SqlParameter[] parameters = {
                        new SqlParameter("@Account", Account),
                        new SqlParameter("@PassWord", password)
                };
    
                MTManagementTools.Model.ToolMember model = new MTManagementTools.Model.ToolMember();
                DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return DataRowToModel(ds.Tables[0].Rows[0]);
                }
                else
                {
                    return null;
                }
            }
    View Code

    前台视图:

    视图传给控制器

    控制器:

    1、控制器参数的名字和前台name的名字对应上,就会自动识别

    2、控制器参数如果是对象,对象属性的和name对应上,也会自动识别

     前台传递

    当一个控制器有两个同名方法的时候  在方法上一行加上标记用来区分[httpGet]  [HttpPost] 

    Shared文件,所有的控制器都可以使用的文件夹,母版页就放在这 文件内

    传值有好几种方式  viewbag viewdata  tempdate  model

    同ViewData和ViewBag一样,TempData也可以用来向视图传递数据。只是ViewData和ViewBag的生命周期和View相同,只对当前View有用。而TempData则可以在不同的Action中进行传值,类似webform里的Seesion。
    TempData["abc"] = "this is tempdata";
    <p>@TempData["abc"]</p>

    跳转

    return RedirectToAction("action","controller")
    return RedirectToAction("action","controller",RouteValue)
    
    比如:
    return RedirectToAction("Index","Home")
    return RedirectToAction("Index","Home",new {参数1=xx,参数2=xxx})

     11、区分post get请求

    在控制器上加标签[HttpPost] (只能接收psot请求)  或者 [get]

    12、MVC自带的校验

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    
    namespace Monitor_Web.Models.AccountManager
    {
        public class AddUserViewModel
        {
            [Required(ErrorMessage = "必填字段")]
            [Display(Name = "用户名")]
            [MaxLength(30, ErrorMessage = "用户名长度最大为{0}个字符")]
            //[Remote("CheckUserNameServer", "Account", ErrorMessage = "用户名已存在")]
            public string UserName { get; set; }
    
            [Required(ErrorMessage = "必填字段")]
            [Display(Name = "密码")]
            public string UserPwd { get; set; }
    
            [Required(ErrorMessage = "必填字段")]
            [System.ComponentModel.DataAnnotations.Compare("UserPwd", ErrorMessage = "密码不一致")]
            [Display(Name = "确认密码")]
            public string ConfirmPwd { get; set; }
    
            [Required(ErrorMessage = "必填字段")]
            [MaxLength(100, ErrorMessage = "全名长度最大为{0}个字符")]
            [Display(Name = "全名")]
            public string FullName { get; set; }
    
            [Required(ErrorMessage = "必填字段")]
            [RegularExpression(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", ErrorMessage = "请输入正确的电子邮箱地址!")]
            [Display(Name = "邮箱")]
            public string MailBox { get; set; }
    
            [Display(Name = "管理域")]
            public string DomainIds { get; set; }
    
            [Display(Name = "角色")]
            public long RoleId { get; set; }
    
            [Display(Name = "状态")]
            public bool Status { get; set; }
    
            [Display(Name = "是否能删除")]
            public bool CanDelete { get; set; }
        }
    }
    View Code

     13、html.beginform    自带的ajax表单 beiginajaxfrom

    14、母版页

    @RenderBody()  :只能添加一个此方法,如果还想多挖几个坑,用下面的方法

    @RenderSection("Name")、@RenderSection("Name",false) 第二个参数是指这个坑是否必填,默认true 必填

    15、DB.UserInfo. 点不出where

    没有引用entity的dll 而且dal和model的版本dll一定要一直,版本不一致解决起来非常麻烦,建议删除重建

    NuGet 配置文件信息packages.config

    16、在MVC的controller中不能直接跳转,控制器必须有一个返回结果(进行基类验证的时候,正常控制器中可以)

    //  filterContext.HttpContext.Response.Redirect("/Login/Index");
     filterContext.Result = Redirect("/Login/Index");//注意.
    using CZBK.ItcastOA.IBLL;
    using CZBK.ItcastOA.Model;
    using Spring.Context;
    using Spring.Context.Support;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace CZBK.ItcastOA.WebApp.Controllers
    {
        public class BaseController : Controller
        {
            
            public UserInfo LoginUser { get; set; }
            /// <summary>
            /// 执行控制器中的方法之前先执行该方法。
            /// </summary>
            /// <param name="filterContext"></param>
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                base.OnActionExecuting(filterContext);
                //if (Session["userInfo"] == null)
                bool isSucess = false;
                if(Request.Cookies["sessionId"]!=null)
                {
                    string sessionId = Request.Cookies["sessionId"].Value;
                    //根据该值查Memcache.
                    object obj=Common.MemcacheHelper.Get(sessionId);
                    if(obj!=null)
                    {
                        UserInfo userInfo = Common.SerializeHelper.DeserializeToObject<UserInfo>(obj.ToString());
                       LoginUser = userInfo;
                       isSucess = true;
                       Common.MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20));//模拟出滑动过期时间.
                        //留一个后门,测试方便。发布的时候一定要删除该代码。
                       if (LoginUser.UName == "itcast")
                       {
                           return;
                       }
                        //完成权限校验。
                        //获取用户请求的URL地址.
                       string url = Request.Url.AbsolutePath.ToLower();
                        //获取请求的方式.
                       string httpMehotd = Request.HttpMethod;
                        //根据获取的URL地址与请求的方式查询权限表。
                       IApplicationContext ctx = ContextRegistry.GetContext();
                       IBLL.IActionInfoService ActionInfoService = (IBLL.IActionInfoService)ctx.GetObject("ActionInfoService");
                      var actionInfo= ActionInfoService.LoadEntities(a=>a.Url==url&&a.HttpMethod==httpMehotd).FirstOrDefault();
                      if (actionInfo != null)
                      {
                          filterContext.Result = Redirect("/Error.html");
                          return;
                      }
    
                        //判断用户是否具有所访问的地址对应的权限
                       IUserInfoService UserInfoService = (IUserInfoService)ctx.GetObject("UserInfoService");
                       var loginUserInfo = UserInfoService.LoadEntities(u=>u.ID==LoginUser.ID).FirstOrDefault();
                        //1:可以先按照用户权限这条线进行过滤。
                       var isExt =(from a in loginUserInfo.R_UserInfo_ActionInfo
                                   where a.ActionInfoID == actionInfo.ID
                                   select a).FirstOrDefault();
                       if (isExt != null)
                       {
                           if (isExt.IsPass)
                           {
                               return;
                           }
                           else
                           {
                               filterContext.Result = Redirect("/Error.html");
                               return;
                           }
    
                       }
                        //2:按照用户角色权限这条线进行过滤。
                       var loginUserRole = loginUserInfo.RoleInfo;
                       var count = (from r in loginUserRole
                                   from a in r.ActionInfo
                                   where a.ID == actionInfo.ID
                                   select a).Count();
                       if (count < 1)
                       {
                           filterContext.Result = Redirect("/Error.html");
                           return;
                       }
                        
    
                    }
                   
                   
    
                  
                   
                }
                if (!isSucess)
                {
                    //  filterContext.HttpContext.Response.Redirect("/Login/Index");
                    filterContext.Result = Redirect("/Login/Index");//注意.
                }
            }
        }
    }
    View Code

    17、controller调用两次

    不知道为什么 引用路径写错了 就会调用两次controller
    导致报错,空值,找不到对象等
    错误:

    @Styles.Render("ueditorcss")
    @Scripts.Render("~/bundles/ueditorjs")

    正确:

    @Styles.Render("~/bundles/ueditorcss")
    @Scripts.Render("~/bundles/ueditorjs")

    网上说如果有img元素,并且元素的src值是 ""或者# 也会调用两次,(我没发现)

    MVC区域 如果在区域中调用/跳转非区域的控制器

    如果是在根目录下area就写空

    如果是在区域下area就写对应的area名字

    <a href="@Url.Action("Detail", "../Article", new { id = item.ID })" target="_blank">阅读全文</a>
    <li><a  href="@Url.Action("Index","Home", new {area="" })"><span>首页</span></a></li>
    <li><a  href="@Url.Action("Index", "Goods", new {area="areaname" })"><span>其他信息</span></a></li>

     csrf

    • 在cshtml页面的form标签里加上@Html.AntiForgeryToken()
    • 在Controller需要防范CSRF的Action上加上[ValidateAntiForgeryToken]注解
  • 相关阅读:
    数据结构-树与二叉树-思维导图
    The last packet successfully received from the server was 2,272 milliseconds ago. The last packet sent successfully to the server was 2,258 milliseconds ago.
    idea连接mysql报错Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property
    redis学习笔记
    AJAX校验注册用户名是否存在
    AJAX学习笔记
    JSON学习笔记
    JQuery基础知识学习笔记
    Filter、Listener学习笔记
    三层架构学习笔记
  • 原文地址:https://www.cnblogs.com/xiaoshi657/p/4619853.html
Copyright © 2011-2022 走看看