Rm558
Rm558

Reputation: 5002

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!

Upvotes: 4

Views: 7073

Answers (2)

Anonymous
Anonymous

Reputation: 1

<%@ 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()))

Upvotes: 0

Rm558
Rm558

Reputation: 5002

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;
        }
    }
}

Upvotes: 6

Related Questions