Note

1.Razor渲染顺序/事件(Ajax)执行顺序

1.Razor渲染顺序/事件(Ajax)执行顺序

The Razor pipeline is:

 
  
  1. First, Razor evaluates, if present, _ViewStart.cshtml that contains only Razor statements (C# or VB) for assign Layout or other initialization, it should never have html tags inside.

  2. Then, it parse and evaluates the "View" cshtml file.

  3. Then, it parse and evaluates, if present, the Layout, and when evaluates the @RenderBody method of the cshtml layout file, replaces it with the html script resulting from evaluation of "View" cshtml file.

  4. Finally, it builds the html control graph objects of layout and view html files.

//图片验证码
<img id="imgcode" src="@Url.Action("SecurityCode", "Agent")" />

//不能直接用@Session[SecurityCode]获取验证码的值
//HTML加载完成后, Razor会先从上到下渲染变量(图片处留空),最后进入图片src的路径
//这会导致图片加载完成前Session就取到了值(第一次为空,第二次开始取上一个验证码的值)
            $('#imgcode').mousedown(function () {

                this.src = this.src + '?';//刷新图片
            });

//这个异步方法有几率在图片刷新之前完成,所以不用click
            $('#imgcode').mouseup(function () {
                $.get('@Url.Action("SecurityCode", "Agent")')
                    .done(function (data) {
                        code = data.sessioncode;
                        //alert(code);
                    })
                    .fail(function (data) {
                        alert('Ajax code request fail');
                        alert(JSON.stringify(data));
                    });
            });

//后端代码
public ActionResult SecurityCode()
        {
            if (!Request.IsAjaxRequest())
            {
                string code = CreateRandomCode(4); 
                Session["SecurityCode"] = code; return File(CreateValidateGraphic(code), "image/Jpeg");
            }
            else
            {
                var CodeObject = new { sessioncode = Session["SecurityCode"] as String };
                return Json(CodeObject, JsonRequestBehavior.AllowGet);
            }
        }
 

 

转载于:https://www.cnblogs.com/Jayesslee/p/9213828.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值