zoukankan      html  css  js  c++  java
  • MVC应用积累

    1、Controller中的跳转

    (1)直接Redirect后加(Controller/Action):Response.Redirect("/Home/Index");  

    (2)直接return后加(Controller/Action):return Redirect("/Home/Index");  

    (3)使用RedirectToAction方法

            [1]同一个Controller中,直接跳到一个action:return RedirectToAction("Index");  

            [2]不在同一Controller中:return RedirectToAction("Index","Home");

    2、基类Controller的登录验证

    public class ControllerBase : Controller
        {
            /// <summary>
            /// 重写控制器初始化
            /// </summary>
            protected override void Initialize(System.Web.Routing.RequestContext requestContext)
            {
                base.Initialize(requestContext);
                if (Session["UserID"] == null)
                {
                    //验证是否登陆后跳转
                    Response.Redirect("~/Home/Login");
                }
            }
    
            /// <summary>
            /// 使用FilterAction,重写其
            /// </summary>
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                //加载已登录用户对象和Style数据, 由子类实现
                if (Session["UserID"] == null)
                {
                    filterContext.Result = RedirectToRoute("Default", new { Controller = "Home", Action = "Login" });
                    return;
                }
            }
        }
    View Code

    3、MVC4.0实现Response.Write()等同效果:

    [1]方法一:  @{Output.Write("aaaa");}

    [2]方式二:public ActionResult myAction() { Return Content("Hello World!"); }

    4、显示富文本编辑器编辑过的带格式文本

    [1]直接显示:<%: ViewData.Eval("zzModel.D_Content") %>

        显示结果:<p>合格&nbsp; 深文巧诋</p>

    [2]带格式显示:<%= Server.HtmlDecode(ViewData.Eval("zzModel.D_Content").ToString()) %>

        或 <%= HttpUtility.HtmlDecode(ViewData.Eval("zzModel.D_Content").ToString()) %>

        显示结果:合格  深文巧诋

    [3]Razor 语法下带格式显示:@Html.Raw(Server.HtmlDecode("zzModel.D_Content"))

        或 @Html.Raw(HttpUtility.HtmlDecode("zzModel.D_Content"))

  • 相关阅读:
    php极光网络一键登录(yii框架)
    Sublime Text3将多行转为为一行 | Sublime Text 快速分别独立选中多行
    mysql 将时间戳转换成日期格式
    Vant主题定制修改颜色样式
    TypeError: this.getOptions is not a function 引入less一直报错
    export defaul 和 export定义和区别
    Vue vant引入,tabbar封装使用示例
    php去除富文本编辑器中的内容格式
    ES6:高级数组函数,filter/map/reduce
    [BZOJ2793][Poi2012]Vouchers
  • 原文地址:https://www.cnblogs.com/xinaixia/p/4070495.html
Copyright © 2011-2022 走看看