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);
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章