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

  • 相关阅读:
    根据自己的博客数据统计国内IT人群
    使用dropwizard(5)--加入swagger
    使用dropwizard(4)-加入测试-jacoco代码覆盖率
    使用dropwizard(3)-加入DI-dagger2
    收藏博客
    IntelliJ IDEA 下载安装(含注册码)
    fontawesome图标字体库组件在服务器上显示不出来图标的解决
    MySQL DBA工作角色和职责介绍
    MySQL主主复制(双主复制)配置过程介绍
    MySQL表与表之间的SQL Joins图介绍
  • 原文地址:https://www.cnblogs.com/xinaixia/p/4070495.html
Copyright © 2011-2022 走看看