Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Override Response Content-Type with OutputCache

Response content-type will be set as text/html on OutputCache is assigned, even if there is an assignment inside action.

HomeController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace WebApplication1.Controllers
  8. {
  9.     public class HomeController : Controller
  10.     {
  11.         public ActionResult Index()
  12.         {
  13.             return View();
  14.         }
  15.  
  16.         [OutputCache(Duration = 60)]
  17.         public ActionResult NavigationScript()
  18.         {
  19.             Response.ContentType = "application/javascript";
  20.             return View();
  21.         }
  22.     }
  23. }
Result

Custom a attribute and change content type after output cache processed.

ResponseHeaderAttribute.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace WebApplication1.Filter
  8. {
  9.     public class ResponseHeaderAttribute : ActionFilterAttribute
  10.     {
  11.         public string ContentType { get; set; }
  12.         public System.Text.Encoding ContentEncoding { get; set; }
  13.  
  14.         public override void OnResultExecuted(ResultExecutedContext filterContext)
  15.         {
  16.             if (!string.IsNullOrWhiteSpace(ContentType)) filterContext.HttpContext.Response.ContentType = ContentType;
  17.             if (ContentEncoding != null) filterContext.HttpContext.Response.ContentEncoding = ContentEncoding;
  18.         }
  19.     }
  20. }
HomeController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using WebApplication1.Filter;
  7.  
  8. namespace WebApplication1.Controllers
  9. {
  10.     public class HomeController : Controller
  11.     {
  12.         public ActionResult Index()
  13.         {
  14.             return View();
  15.         }
  16.  
  17.         [OutputCache(Duration = 60, Order = 1)]
  18.         [ResponseHeader(ContentType = "application/javascript", Order = 2)]
  19.         public ActionResult NavigationScript()
  20.         {
  21.             return View();
  22.         }
  23.     }
  24. }
Result