Asp.net MVC Render及Redirect的扩展
这个是Redirect的扩展
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);
}
}
}
}
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 + "/");
}
}
}
{
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);
}
}
}
}