locked
Identify axd file content type? RRS feed

  • Question

  • User-824764785 posted

    I've successfully created a module to minify asp.net default axd files that render only javascripts, but with AjaxControlToolkit I need to differentiate the content type weather it is css or image or javascript, please not that (Request/Response).ContentType is returning text/html

    public class ParseAXD : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += context_BeginRequest;
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            var context = HttpContext.Current;
            var path = context.Request.Url.PathAndQuery;
            if (path.ToLower().Contains(".axd") && IfNotWebClientRequest())
            {
                var remote = new Uri(Lib.SiteDb.DomainName + path);
                using (WebClient client = new WebClient())
                {
                    string content = client.DownloadString(remote);

    //Need to identify the ContentType here, please not that (Request/Response).ContentType is returning text/html
    var settings = new CodeSettings(); settings.MinifyCode = true; settings.LocalRenaming = LocalRenaming.CrunchAll; settings.RemoveFunctionExpressionNames = true; settings.EvalTreatment = EvalTreatment.MakeAllSafe; content = new Minifier().MinifyJavaScript(content, settings); context.Response.ContentType = "text/javascript"; context.Response.Write(content); context.Response.Cache.SetValidUntilExpires(true); context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1)); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetOmitVaryStar(true); context.Response.Flush(); context.Response.End(); } } }

    Sunday, March 29, 2015 2:26 PM

Answers

  • User-824764785 posted

    I already mentioned above that Response.ContentType is coming text/html for each and every .axd file.

    Anyhow I found a solution, and posted in my blog here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 30, 2015 4:48 AM

All replies

  • User1711366110 posted

    vikasrulez

    I've successfully created a module to minify asp.net default axd files that render only javascripts, but with AjaxControlToolkit I need to differentiate the content type weather it is css or image or javascript, please not that (Request/Response).ContentType is returning text/html

      As per your case, you can try like below codings :

    public class ParseAXD : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += context_BeginRequest;
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            var context = HttpContext.Current;
            var path = context.Request.Url.PathAndQuery;
            if (path.ToLower().Contains(".axd") && IfNotWebClientRequest())
            {
                var remote = new Uri(Lib.SiteDb.DomainName + path);
                using (WebClient client = new WebClient())
                {
                    //string content = client.DownloadString(remote);
    				string contentType =  Response.ContentType;
    				switch (contentType)
                    {
                      case "text/javascript":
                      
                    {
    
                    //Need to identify the ContentType here, please not that (Request/Response).ContentType is returning text/html
    
                    var settings = new CodeSettings();
                    settings.MinifyCode = true;
                    settings.LocalRenaming = LocalRenaming.CrunchAll;
                    settings.RemoveFunctionExpressionNames = true;
                    settings.EvalTreatment = EvalTreatment.MakeAllSafe;
                    content = new Minifier().MinifyJavaScript(content, settings);
                    context.Response.ContentType = "text/javascript";
                    context.Response.Write(content);
    
                    context.Response.Cache.SetValidUntilExpires(true);
                    context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);
                    context.Response.Cache.SetOmitVaryStar(true);
    
                    context.Response.Flush();
                    context.Response.End();
    				
    				break;
    				}
    case "text/css":
    {
    ......
    break;
    }
                      case "text/html":
    {
                       ...... 
                        break;
                       } } } }

    --
    with regards,
    Edwin

    Monday, March 30, 2015 4:05 AM
  • User-824764785 posted

    I already mentioned above that Response.ContentType is coming text/html for each and every .axd file.

    Anyhow I found a solution, and posted in my blog here

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 30, 2015 4:48 AM