Search code examples
asp.netdynamiccompilationashx

how to dynamically compile a ashx file?


I have a ashx file in a asp.net web project. It has a code behind cs file. The cs file is compiled into project dll & deployed to production. I found no way to dynamically change the cs file without deploying the whole project dll.

I have been putting c# code into aspx (not .cs) file, after deployment, I can make change to a single aspx file, and deploy, IIS can dynamically compile & merge with its code behind c# code.

Can I do the similar thing with ashx file?

Here is a quote from MSDN. http://msdn.microsoft.com/en-us/library/ms366723(v=vs.100).aspx

ASP.NET supports the dynamic compilation of ASP.NET pages (.aspx files), ASP.NET Web services (.asmx files), ASP.NET HTTP handlers (.ashx files)

thanks, this can save me lots of time!


Solution

  • I figured it out.

    1. add a Generic handler - Handler1.ashx in visual studio
    2. delete the cs file which auto-created.
    3. open ashx again,
      • remove CodeBehind="Handler1.ashx.cs"
      • add c# code below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    public class Handler1 : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World2");
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }