• 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);
                }
            }
        }
    }
  • 相关阅读:
    个人软件过程详细需求分析
    班级网站分析
    PSP个人软件开发系统面向对象需求分析与设计文档
    Psp个人软件开发软件需求分析及用例分析
    上海期货交易所数据备份软件项目前景与范围文档
    vmware虚拟机与主机连接
    c#实现截取电脑全屏
    求一串数组的最大的子数组的和 孔祥安AND杨霏
    多路电梯调度问题 杨霏AND孔祥安
    分析文件中频率出现最多的前十个词
  • 原文地址:https://www.cnblogs.com/chsword/p/AspnetExtRenderRedirect.html
Copyright © 2020-2023  润新知