【问题标题】:how to dynamically compile a ashx file?如何动态编译ashx文件?
【发布时间】:2014-10-20 00:52:52
【问题描述】:

我在一个 asp.net 网络项目中有一个 ashx 文件。它在cs文件后面有一个代码。 cs 文件被编译成项目 dll 并部署到生产环境中。在不部署整个项目 dll 的情况下,我发现无法动态更改 cs 文件。

我一直将 c# 代码放入 aspx(不是 .cs)文件中,部署后,我可以对单个 aspx 文件进行更改,然后部署,IIS 可以动态编译和合并 c# 代码后面的代码。

我可以用 ashx 文件做类似的事情吗?

这是来自 MSDN 的引用。 http://msdn.microsoft.com/en-us/library/ms366723(v=vs.100).aspx

ASP.NET 支持 ASP.NET 页面(.aspx 文件)、ASP.NET Web 服务(.asmx 文件)、ASP.NET HTTP 处理程序(.ashx 文件)的动态编译

谢谢,这可以节省我很多时间!

【问题讨论】:

  • 您可能会考虑切换到网站项目(而不是 Web 应用程序项目)。或者查看 ASP.NET vNext,它是 ASP.NET 的下一个主要版本,它将有一个混合项目系统,结合了两者的最佳特性。
  • @user2055187 频繁更新代码隐藏文件的原因是什么,并且不想更新项目dll?
  • @mason vNext 充其量只是测试版,功能不完整,请不要将其用于生产。
  • @siva.k 我不建议将它用于生产。我说要调查一下。
  • 谢谢大家,更新项目dll是我公司流程中的“正式”新版本发布。单个 ashx 文件部署“更轻”,在我的控制之下。

标签: asp.net dynamic compilation ashx


【解决方案1】:

我想通了。

  1. 在 Visual Studio 中添加通用处理程序 - Handler1.ashx
  2. 删除自动创建的cs文件。
  3. 再次打开ashx,
    • 删除 CodeBehind="Handler1.ashx.cs"
    • 在下面添加c#代码

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

【讨论】:

  • 应用我在此处学到的知识,请参阅 this answer 了解我创建的用于在 ASP.NET Web 应用程序中测试我的 Oracle 连接的完整工作示例。
【解决方案2】:
<%@ 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()))

【讨论】:

  • 欢迎来到 StackOverflow!您能否描述您的答案,以便为您提出的解决方案提供更多背景信息?这将帮助 OP 和未来的读者了解这如何解决 OP 的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 2021-03-13
  • 1970-01-01
  • 2011-03-27
相关资源
最近更新 更多