简体   繁体   English

ASP.NET C#中的自定义处理程序/控件的代码重用

[英]Code Reuse of Custom Handlers/Controls in ASP.NET C#

I recently decided that I would write a custom ASP.NET web control to take a preview of a website and display it as an image. 我最近决定,我将编写一个自定义ASP.NET Web控件来预览网站并将其显示为图像。 I found various methods of doing this on the internet and took my favorite parts from each. 我在互联网上找到了执行此操作的各种方法,并从每种方法中获得了我最喜欢的部分。

The first revision of the web control worked well enough but the previews weren't being generated asynchronously so the use of the control was frustrating because of load times. Web控件的第一个修订版本运行良好,但是预览不是异步生成的,因此由于加载时间而使控件的使用令人沮丧。 I wrote another revision of code to generate the previews in different requests (using a handler) and this worked well also. 我编写了另一个代码修订版,以在不同的请求中生成预览(使用处理程序),而且效果也很好。

My third revision was to write a custom control that used the handler so that I could get the convenience of the control and the delayed loading of the handler. 我的第三个修订版是编写一个使用处理程序的自定义控件,这样我就可以方便地使用控件和延迟加载处理程序。 This worked well in its own project. 这在自己的项目中效果很好。 Where it became complicated was when I tried to register the handler from the control library project in a different website project. 当我尝试从另一个网站项目中的控制库项目中注册处理程序时,情况就变得复杂了。

First off, am I going about this the right way? 首先,我要采用正确的方法吗? It seemed like a great idea until I tried to register the handler. 在我尝试注册处理程序之前,这似乎是一个好主意。 Using the control is easy enough. 使用控件非常容易。

Second, if this is the correct method of doing this how do I register this handler? 第二,如果这是执行此操作的正确方法,我该如何注册此处理程序? Inside the web applications web.config I have tried various flavors of class names and namespaces but I always get a 404 when the control tries to load the image from the handler. 在Web应用程序web.config中,我尝试了各种类型的类名和名称空间,但是当控件尝试从处理程序中加载图像时,我总是得到404。

Here's what I have right now to get an idea: 这是我现在有一个想法:

<add verb="GET" name="ThumbnailHandler" path="GenerateWebsitePreview.ashx" type="HandlerGenerateWebsitePreview, ThumbnailControl"/>

Name: This doesn't matter as far as I can tell. 名称:就我所知,这无关紧要。 It's not required in any of the documentation I've read and is almost never mentioned. 在我阅读的任何文档中都不需要它,并且几乎从未提及过。 Path: This is the path/url I want to invoke the handler. 路径:这是我要调用处理程序的路径/ URL。 This does not exist in the web application project obviously nor should it. Web应用程序项目中显然不存在此功能,也不应该存在。 I want it to reuse the one from the library. 我希望它重新使用库中的那个。 With handlers you can use whatever path you want though. 使用处理程序,您可以使用所需的任何路径。 Type: HandlerGenerateWebsitePreview is the name of the class that implements IHttpHandler. 类型:HandlerGenerateWebsitePreview是实现IHttpHandler的类的名称。 ThumbnailControl is the namespace that this class sits in. ThumbnailControl是此类所在的名称空间。

The web application already has a project reference to the library project that contains the custom control and the custom handler. 该Web应用程序已经具有对包含自定义控件和自定义处理程序的库项目的项目引用。 This reference is known to be working because the custom controls are found and rendered. 已知此引用正在工作,因为找到并呈现了自定义控件。

How do you reuse custom handlers if you can't register a handler from another project? 如果无法从另一个项目注册处理程序,该如何重用自定义处理程序? The "assembly" attribute isn't allowed in the "add" tag like it is when you register the custom control. “ add”标签中不允许使用“ assembly”属性,就像注册自定义控件时一样。

Thanks in advance! 提前致谢!

I should have spent a few more minutes trying assembly names, namespaces and class names. 我应该花几分钟的时间来尝试程序集名称,名称空间和类名称。 I tried again today and this combination worked where nothing else would. 我今天再试一次,这种组合在其他任何地方都无法奏效。

<add verb="GET" name="Name" path="path.path" type="Namespace.ClassName, AssemblyName"/>

I thought I had tried this combination but apparently I didn't. 我以为我尝试过这种组合,但显然没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义ASP.NET控件中的Begin / EndRequestHandler多个处理程序 - Begin/EndRequestHandler multiple handlers in custom ASP.NET controls 在ASP.Net C#中手动添加页面事件处理程序 - Manually adding page event handlers in ASP.Net C# 模块化和重用asp.net C#应用程序 - Modularize and Reuse asp.net C# application 重用令牌asp.net web api c# - reuse token asp.net web api c# (动态数据,ASP.Net,C#,下拉列表自定义.ascx控件)如何在下拉列表之间传递值? - (Dynamic Data, ASP.Net, C#, dropdownlists custom .ascx controls ) HOW TO PASS VALUES AMONG DROPDOWNLISTS? 完全卡住,简短地在标题中解释,涵盖了c#asp.net控件和HTML代码以及一些小的javascript - Utterly stuck, to short to explain in a title, covers c# asp.net controls and html code and a little javascript 从文件后面的代码向ASP.NET C#中的Listview控件动态添加子控件 - Dynamically adding child controls to listview control in ASP.NET C# from code behind file ASP.Net C#-页面上有多个UpdateProgress控件 - ASP.Net C# - Multiple UpdateProgress controls on a page 访问动态创建的控件c#asp.net的值 - Accessing value of dynamically created controls c# asp.net 动态添加控件和事件处理程序(asp.net C#) - Dynamically adding controls and eventhandlers (asp.net c#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM