Asp.net MVC Render and Redirect Extension

Source: Internet
Author: User
This is an extension of Redirect. Namespace System. Web. Mvc
{
Using System;
/// <Summary>
/// Extension of the Controller Redirect operation
// Blog: http://chsword.cnblogs.com/
/// </Summary>
Public static class RedirectExtension
{
/// <Summary>
/// Redirect to the previous Action. That is, the header's "HTTP_REFERER" (<c> Context. UrlReferrer </c> ).
/// </Summary>
Static public void RedirectToReferrer (this Controller controller ){
Controller. Response. Redirect (controller. Request. ServerVariables ["HTTP_REFERER"]);
}
[Obsolete ("expired please use RedirectToReferrer")]
Static public void RedirectToReferer (this Controller controller)
{
RedirectToReferrer (controller );
}
/// <Summary>
/// Redirect to the site root directory (<c> Context. ApplicationPath + "/" </c> ).
/// </Summary>
Static public void RedirectToSiteRoot (this Controller controller ){
Controller. Response. Redirect (controller. Request. ApplicationPath + "/");
}

}
}

Pv3 already exists, but it is not supported by void, but it still has its availability.

Namespace System. Web. Mvc
{
Using System;
Using System. Text;
Using System. Web. Script. Serialization;
Using System. Runtime. Serialization. Json;
/// <Summary>
/// Extension of RenderView
// Blog: http://chsword.cnblogs.com/
/// </Summary>
Static public class RenderExtension
{
/// <Summary>
/// Display the text to be displayed
/// </Summary>
/// <Param name = "c"> </param>
/// <Param name = "str"> text content </param>
[Obsolete ("used only in Asp.net Mvc Preview2, and a new method Content is provided in PV3")]
Static public void RenderText (this Controller c, string str)
{
C. HttpContext. Response. Write (str );
}
/// <Summary>
/// Return the object to be displayed in JSON to the client
/// </Summary>
/// <Param name = "c"> </param>
/// <Param name = "data"> object to be sent </param>
[Obsolete ("used only in Asp.net Mvc Preview2, and a new method Json is provided in PV3")]
Public static void RenderJSON (this Controller c, object data)
{
C. RenderJSON (data, null );
}
/// <Summary>
/// Return the object to be displayed in JSON to the client
/// </Summary>
/// <Param name = "c"> </param>
/// <Param name = "data"> object to be sent </param>
/// <Param name = "contenttype"> the default Content-Type transmitted is application/json. </param>
[Obsolete ("used only in Asp.net Mvc Preview2, and a new method Json is provided in PV3")]
Public static void RenderJSON (this Controller c, object data, string contenttype)
{
C. RenderJSON (data, contentType, null );
}
/// <Summary>
/// Return the object to be displayed in JSON to the client
/// </Summary>
/// <Param name = "c"> </param>
/// <Param name = "data"> object to be sent </param>
/// <Param name = "contenttype"> If the transmitted Content-Type is null, the default value is application/json. </param>
/// <Param name = "encoding"> encoding method </param>
[Obsolete ("used only in Asp.net Mvc Preview2, and a new method Json is provided in PV3")]
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 );
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.