3
votes

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!

2
You might consider switching to a website project (instead of web application project). Or look into ASP.NET vNext, which is the next major release of ASP.NET that will have a hybrid project system that combines the best features of both. - mason
@user2055187 What is the reason behind updating code behind files frequently, and not wanting to update project dll? - Win
@mason vNext is beta at best and not feature complete, please don't recommend that for production. - siva.k
@siva.k I wasn't recommending it for production. I said to look into it. - mason
thanks all, update project dll is a "formal" new version release in my company's process. a single ashx file deployment is "lighter" which under my control. - Rm558

2 Answers

5
votes

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;
        }
    }
}
0
votes
<%@ WebHandler Language="VB" Class="FileVB1" %>

Imports System
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class FileVB1 : Implements IHttpHandler
    
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim Slno As Integer = GlobalVariables.Slno
        Dim bytes As Byte()
        Dim fileName As String, contentType As String
        Dim constr As String = ConfigurationManager.ConnectionStrings("APMCUBIntranetConnectionString").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand()
                cmd.CommandText = "SELECT Slno,FileName, Valid FROM Manuals WHERE Slno=@Id"
                cmd.Parameters.AddWithValue("@Id", Slno)
                cmd.Connection = con
                con.Open()
                Using sdr As SqlDataReader = cmd.ExecuteReader()
                    sdr.Read()
                    Bytes = DirectCast(sdr("Valid"), Byte())
                    contentType = "application/pdf"
                    fileName = sdr("FileName").ToString()
                End Using
                con.Close()
            End Using
        End Using

        context.Response.Buffer = True
        context.Response.Charset = ""
        If context.Request.QueryString("download") = "1" Then
            context.Response.AppendHeader("Content-Disposition", Convert.ToString("attachment; filename=") & fileName)
        End If
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
        context.Response.ContentType = "application/pdf"
        context.Response.BinaryWrite(bytes)
        context.Response.Flush()
        context.Response.[End]()
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class











Please solve my problem i'm  getting error  at  (Bytes = DirectCast(sdr("Valid"), Byte()))