• 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"))

  • 相关阅读:
    匈牙利算法
    Tabbed Activity = viewpager + fragment ?
    gdb调试多线程多进程
    gdb 调试,当发现程序退出,需要定位程序退出位置时。
    将Linux的信号量sem_t封装成事件对象
    Golang包管理工具govendor的使用&go mod
    go get命令详解
    GoLand生成可执行文件(Windows、Linux)
    Linux下线程pid和tid
    理解Linux的进程,线程,PID,LWP,TID,TGID
  • 原文地址:https://www.cnblogs.com/xinaixia/p/4070495.html
Copyright © 2020-2023  润新知