3

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!

Rm558
  • 4,621
  • 3
  • 38
  • 43
  • 1
    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 Aug 26 '14 at 16:08
  • 1
    @user2055187 What is the reason behind updating code behind files frequently, and not wanting to update project dll? – Win Aug 26 '14 at 16:12
  • 1
    @mason vNext is beta at best and not feature complete, please don't recommend that for production. – siva.k Aug 26 '14 at 16:14
  • 1
    @siva.k I wasn't recommending it for production. I said to look into it. – mason Aug 26 '14 at 16:18
  • 2
    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 Aug 26 '14 at 16:58

2 Answers2

5

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;
        }
    }
}
Rm558
  • 4,621
  • 3
  • 38
  • 43
  • Applying what I learned here, see [this answer](http://stackoverflow.com/a/26108116/1497596) for a complete working example that I created to test my Oracle connection within an ASP.NET web application. – DavidRR Sep 29 '14 at 21:01
0
<%@ 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()))
  • 1
    Welcome to StackOverflow! Can you describe your answer so there is more context to your proposed resolution? This will help the OP and future readers understand how this solves the OP's problem. Thanks! – interesting-name-here Jul 24 '20 at 09:56
  • While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Shawn Hemelstrand Jan 28 '23 at 01:45