zoukankan      html  css  js  c++  java
  • Asp.net MVC Render及Redirect的扩展

    这个是Redirect的扩展
    namespace System.Web.Mvc
    {
        
    using System;
        
    /// <summary>
        
    /// 对Controller的Redirect操作的扩展
        
    /// blog:http://chsword.cnblogs.com/
        
    /// </summary>
        public static class RedirectExtension
        {
            
    /// <summary>
            
    /// 重定向到上一个Action. 即 header 的 "HTTP_REFERER"  (<c>Context.UrlReferrer</c>).
            
    /// </summary>
            static public void RedirectToReferrer(this Controller controller) {
                controller.Response.Redirect(controller.Request.ServerVariables[
    "HTTP_REFERER"]);
            }
            [Obsolete(
    "已经过时请使用RedirectToReferrer")]
            
    static public void RedirectToReferer(this Controller controller)
            {
                RedirectToReferrer(controller);
            }
            
    /// <summary> 
            
    /// Redirect 到站点根目录 (<c>Context.ApplicationPath + "/"</c>).
            
    /// </summary>
            static public void RedirectToSiteRoot(this Controller controller) {
                controller.Response.Redirect(controller.Request.ApplicationPath 
    + "/");
            }

        }
    }

    Pv3中已经有了,不过void的情况下不支持,还是有其可用性的


    namespace
     System.Web.Mvc
    {
        
    using System;
        
    using System.Text;
        
    using System.Web.Script.Serialization;
        
    using System.Runtime.Serialization.Json;
        
    /// <summary>
        
    /// 对RenderView的扩展
        
    /// blog:http://chsword.cnblogs.com/
        
    /// </summary>
        static public class RenderExtension
        {
            
    /// <summary>
            
    /// 显示要显示的文本
            
    /// </summary>
            
    /// <param name="c"></param>
            
    /// <param name="str">文本内容</param>
            [Obsolete("仅在Asp.net Mvc Preview2中使用,PV3中已经提供新的方法Content")]
            
    static public void RenderText(this Controller c, string str)
            {
                c.HttpContext.Response.Write(str);
            }
            
    /// <summary>
            
    /// 将要显示的对象以JSON返回要客户端
            
    /// </summary>
            
    /// <param name="c"></param>
            
    /// <param name="data">要发送的对象</param>
            [Obsolete("仅在Asp.net Mvc Preview2中使用,PV3中已经提供新的方法Json")]
            
    public static void RenderJSON(this Controller c, object data)
            {
               
    c.RenderJSON(data, null);
            }
            
    /// <summary>
            
    /// 将要显示的对象以JSON返回要客户端
            
    /// </summary>
            
    /// <param name="c"></param>
            
    /// <param name="data">要发送的对象</param>
            
    /// <param name="contenttype">传送的Content-Type默认为application/json</param>
            [Obsolete("仅在Asp.net Mvc Preview2中使用,PV3中已经提供新的方法Json")]
            
    public static void RenderJSON(this Controller c, object data, string contenttype)
            {
               
    c.RenderJSON(data, contentType, null);
            }
            
    /// <summary>
            
    /// 将要显示的对象以JSON返回要客户端
            
    /// </summary>
            
    /// <param name="c"></param>
            
    /// <param name="data">要发送的对象</param>
            
    /// <param name="contenttype">传送的Content-Type为空则默认为application/json</param>
            
    /// <param name="encoding">编码方式</param>
            [Obsolete("仅在Asp.net Mvc Preview2中使用,PV3中已经提供新的方法Json")]
            
    public static void RenderJSON(this Controller c, object data, string contenttype, Encoding encoding)
            {
                HttpResponseBase response 
    = c.HttpContext.Response;
                
    if (!string.IsNullOrEmpty(contenttype))
                {
                    response.ContentType 
    = contenttype;
                }
                
    else
                {
                    response.ContentType 
    = "application/json";
                }
                
    if (encoding != null)
                {
                    response.ContentEncoding 
    = encoding;
                }
                
    if (data != null)
                {
                    
                DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(object));
                    sr.WriteObject(response.OutputStream, data);
                }
            }
        }
    }
  • 相关阅读:
    RAC一个节点自动重启问题分析
    Oracle Audit 审计总结
    oracle 11g日志审计
    在线剪辑歌曲
    转载:MySQL Cluster NDB(Mysql 集群)
    U 盘多系统安装盘制作神器YUMI
    转载:网站限制IP地址访问-精确到国家/省/市IP地址
    开源企业云盘Seafile部署
    K8S(二)-创建一个pod应用
    mariadb升级
  • 原文地址:https://www.cnblogs.com/chsword/p/AspnetExtRenderRedirect.html
Copyright © 2011-2022 走看看