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);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值