Asp.net MVC Render及Redirect的扩展

简介: 这个是Redirect的扩展namespace System.Web.Mvc {     using System;     ///      /// 对Controller的Redirect操作的扩展     /// blog:http://chsword.
这个是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);
            }
        }
    }
}
相关文章
一款基于 .NET MVC 框架开发、功能全面的MES系统
一款基于 .NET MVC 框架开发、功能全面的MES系统
|
5月前
|
Docker + .NET API:简化部署和扩展
Docker + .NET API:简化部署和扩展
69 1
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
84 7
|
5月前
|
最完美的扩展Docker + .NET API:简化部署和扩展
最完美的扩展Docker + .NET API:简化部署和扩展
127 0
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
123 0
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
87 0
分享一个 .NET EF 6 扩展 Where 的方法
分享一个 .NET EF 6 扩展 Where 的方法
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
252 0
|
7月前
|
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
195 0

热门文章

最新文章