Using Handler in DLL - asp.net

If I open ASP.NET, create a blank site, add a handler file, add a webform then on the webform add an image control and set the imageurl to handler.ashx, all works fine.
Open Visual Studio 2013 or 2015, create a class library project, delete the vb file that's created, add an ASP.NET handler and use the same code as in the above handler.ashx file, compile the code and then add a reference to that DLL from the asp.net project which copies the DLL to the bin folder. My question is, how to use that handler in the asp.net project?
Sure, in the web.config you can add to the hadlers section (not httphandlers) but if registering *.zzz as the extension and then in the imageurl of the image control. ex: "aaaaaa.zzz" it should return a jpg file because of the code in that handler but nothing happens. What am I doing wrong?

I've created a test app to replicate your issue and all works fine for me.
Here is a snippet of my handler configuration, compare it to what you have got:
<system.webServer>
<handlers>
<add name="imageHandler" verb="*" path="*.zzz" type="CustomHandlers.ImageHandler, CustomHandlers"/>
</handlers>
</system.webServer>
if this didn't work, please send the details of the error/exception you are getting.

I am not sure if it is possible to trigger a handler through a dll. What worked for me was using my handler on my local project, and then having the handler call a method from the class library project. That method can then contain all of the logic that is meant to be in your handler.

Related

Owin error with ASP.NET MVC application

I have an ASP.NET application that runs fine on my local machine. I just uploaded it to a server using web deploy. I'm getting the following error when I try to view the site:
The following errors occurred while attempting to load the app.
- The OwinStartup attribute discovered in assembly 'Gators3' referencing startup type 'Gators3.Startup' conflicts with the attribute in assembly 'MyFirstProject2' referencing startup type 'MyFirstProject2.Startup' because they have the same FriendlyName ''. Remove or rename one of the attributes, or reference the desired type directly.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
I tried searching the entire solution for the string "MyFirstProject2" but did not come up with anything. The message gives a couple of suggestions, but none of them mean anything to me. I don't know how to "Remove or rename one of the attributes, or reference the desired type directly," and I don't see a place in the web.config to "add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config."
I found this, but am not quite sure how to implement it. I also did find [assembly: OwinStartupAttribute(typeof(Gators3.Startup))] in my Startup.cs, but not sure what the right thing to do there is either.
The problem is that the Gators3.dll and MyFirstProject2.dll are in the same folder (I guess it is bin folder on your server) and both are using Owin middleware. If you do not need MyFirstProject2.dll then the easiest way would be to delete that assembly. If you need it, but without Owin - add following line to Web.config/app.config in your MyFirstProject2 project:
<add key="owin:AutomaticAppStartup" value="False" />
If you need to use Owin for two projects configure friendly names for both of them.
Gators3 project:
Change Owin startup attribute to something like:
attribute [assembly: OwinStartupAttribute("GatorsConfig", typeof(Gators3.Startup))]
And add following line to Web.config within appSettings section:
<add key="owin:appStartup" value="GatorsConfig" />
MyFirstProject2 project:
Change Owin startup attribute to something
like:
attribute [assembly: OwinStartupAttribute("MyFirstProject2Config", typeof(MyFirstProject2.Startup))]
And add following line to Web.config within appSettings section:
<add key="owin:appStartup" value="MyFirstProject2Config" />
I had the same issue : removing everything in the /bin folder and rebuilding the solution alone worked for me. But it could be combined with renaming your assembly attribute at the top of the startupclass, giving it a Firendly name which will help to differentiate both the startup files.
[assembly: OwinStartup("MyFriendlyNameForProject1",typeof(MyProject.Startup))]
Clear your bin folder and obj folder.Rebuild the project again and run :)
Also, if you publish a solution to Azure:
1) right click and select Publish.
2) go to Settings and expand the "File Publish Options"
3) select "Remove additional files at destination"
4) Publish
Worked for me, after deleting the files from obj and bin Folder.
Delete anything that says 'MyFirstProject2' from your bin folder and rebuild the solution, It will work.
Happens when you reference (by mistake?) a project with owin startup inside another project with owin startup. Fix - delete the reference, bin, obj folders and rebuild.
I had the same problem and i added the following tag on web config:
<appSettings>
<add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
This is what I have done:
Since I have 3 projects in one solution, I had to open all bin folder of each project and delete all files there.
Build each project one by one.
Build the whole solution.
Problem solves on my part.
Remove all files in your 'bin' folder and rebuild.
Delete files Bin, Build each project one by one.
Deleted everything in the bin folder, including the roslyn folder then published, and everything worked fine.
Remove old built data in temporary files, in the following path
C:\Users\[user]\AppData\Local\Temp\Temporary ASP.NET Files\vs
In my case, I was referencing one project in the other accidentally, which is why I was getting this error after removing the one accidentally added in the main project solved the problem.

ASPX page within a .aspx page with iframe throws 500.23 error

I'm trying to embed a ChartModule.aspx page within a Default.aspx page using an iframe.
The ChartModule has a button event which updates a chart. The ChartModule has its own ChartsModule.cs.
I'm getting this error:
HTTP Error 500.23 - Internal Server Error An ASP.NET setting has been
detected that does not apply in Integrated managed pipeline mode.Most
likely causes:
•This application defines configuration in the system.web/httpHandlers
section. Things you can try: •Migrate the configuration to the
system.webServer/handlers section. You can do so manually or by using
AppCmd from the command line. For example, from the IIS Express
install directory, run appcmd migrate config "Default Web Site/".
Using AppCmd to migrate your application will enable it to work in
Integrated mode. It will continue to work in Classic mode and on
previous versions of IIS. •If you are certain that it is OK to ignore
this error, it can be disabled by setting
system.webServer/validation#validateIntegratedModeConfiguration to
false. •Alternatively, switch the application to a Classic mode
application pool. For example, from the IIS Express install directory,
run appcmd set app "Default Web Site/"
/applicationPool:"Clr4ClassicAppPool". Only do this if you are unable
to migrate your application.
Detailed Error Information: Module ConfigurationValidationModule
Notification BeginRequest Handler
PageHandlerFactory-Integrated-4.0 Error Code 0x80070032 Requested
URL http://localhost:4161/Default.aspx Physical Path
C:\Documents and
Settings\singhm\Desktop\Temp\Trial2\Trial2\Default.aspx Logon Method
Not yet determined Logon User Not yet determined Request Tracing
Directory
Why is this?
While this may not answer your question directly, here is a thought:
If you have the option, consider turning ChartModule.aspx into a UserControl (ascx), which acts just like another "page" (same lifecycle, its own codebehind file, etc) but integrates more cleanly into an existing aspx page. The above link should be a good introduction to creating and using UserControls.
The error message contains a clue to the solution:
setting system.webServer/validation#validateIntegratedModeConfiguration to false
So make sure the following is present in your web.config:
<validation validateIntegratedModeConfiguration="false"/>
For example:
<configuration>
<!-- your existing settings -->
<system.webServer>
<!-- Add this to here.... -->
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>
IIS 7 and ASP.NET are quite helpful these days with regard to to error messages and hints contained therein so you should take the time to read them.
I would really recommend using a usercontrol page instead of iframes in asp.net this way you can bind that usercontrol by doing
public override DataBind()
in that you can pass anything into that usercontrol page like refresh data, load certain data, etc..

Adding Assembly to ASP.Net website

I want to use the Task Scheduler Managed Wrapper in my ASP.Net project so that I can access the windows task scheduler from within my website. From what I have learned so far I need to add a reference to the assembly dll to my web.config. is that right? How do I figure out the public key token for the assembly file though? Is there any other way than using the 'sn.exe' utility, because I don't have the .net SDK installed and I unfortunately have no option of doing so on this computer. Thank you for your help!!
You shouldn't have to do anything with sn.exe or the public key token.
If you are working in a Web Site, right click your web-site in the Solution Explorer and click "Add Reference". It will create a Bin folder for you if there isn't one, then add the assembly to the bin.
If you are working in a Web Application, use the Add Reference dialog by right clicking "References" in the solution explorer.
EDIT:
thank you but i am not using visual studio at all. all i have is IIS and I use notepad for editing. the server side portion of my website is written in jscript.net
You will probably be adding a Reference with the web.config then, such as:
<system.web>
<assemblies>
<add assembly="YourAssemblyNameInBin" />
</assemblies>
</system.web>
Place your assembly in the Bin, and change YourAssemblyNameInBin to the name of the assembly on disk without the extension.
You don't have to include the version, culture, and public key token of the assembly. If you want to though, you can use the sn.exe tool to display the public key token like so:
sn.exe -T "PathToAssembly"
Note that the T is capital. If the assembly is not signed - then you must omit the public key token from the reference.

ASP.NET Controls Not Recognized

I just installed the release version of Visual Studio 2010, and it looks like there's something wrong with the standard ASP.NET server controls.
When I create a basic server control, such as a TextBox, the editor underlines it in green and says (I also get no intellisense for ASP.NET controls):
Element 'TextBox' is not a known element. This can occur if there is a compilation
error in the Web site, or the Web.config file is missing.
The web site builds fine, and the Web.config file is not missing:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
</configuration>
The controls work just fine in the code-behind, and they function correctly when I run the site.
Is there some configuration problem here?
This person who's a tester on the Visual Web Developer Team offered this workaround, which apparently worked:
Close VS, delete schema cache, and re-open VS. You can find the schemas under something like:
C:\Users\Pavel\AppData\Roaming\Microsoft\VisualStudio\10.0\ReflectedSchemas
It is safe to delete all files in this folder.
I've encountered the same problem, but deleting the cache didn't solve it for me.
However based on the other answer, I modified the already set master page path in the .aspx file (MasterPageFile="~/MasterPage.master")
to something nonexistent, saved the file, then changed it back to the original path. After this the underlining was gone, and intellisense worked properly.
Funny that this is still a thing 2020. Using VS2019 Community.
Extension on the question as your solution didn't work for me.
I was seeing this in my SharePoint 2010 Application Pages which did not specify a masterpageurl in the Page tag.
Solution: After setting MasterPageURL="~masterurl/default.master" to the Page tag in the designer, it fixed all my ASP tags.
In my case, I noticed the MyPage.aspx.designer.cs was missing. Once i made a copy of existing file and defined the controls, all was set.
Please note for me, there were three files.
MyPage.aspx
MyPage.aspx.cs
MyPage.aspx.designer.cs
The definition of controls looks something like this:
protected global::System.Web.UI.HtmlControls.HtmlInputText txtName;
/// <summary>
/// txtAddress control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputText txtAddress;

Why isn't my IHttpHandler being called?

I'm trying to get a custom handler to work for a specific URL (or set of URLs) in ASP.NET 3.5.
The handler doesn't actually do anything significant yet - it just logs the request. I can post the code if anyone things it's relevant, but I really don't think it's being called at all. (In particular, for normal exceptions I get a custom error page and logging... here I'm just getting the vanilla IIS 404.)
Here's the relevant bit of the web.config file:
<system.web>
<httpHandlers>
<add verb="GET,POST" path="*.robot" validate="false"
type="CSharpInDepth.Wave.RobotHandler, CSharpInDepth"/>
</httpHandlers>
</system.web>
(Obviously there's other stuff in that section too, but I don't think it's relevant.)
Locally, running under the dev server, it works fine. On my real box, I always get a 404. Everything under the web site directory itself is the same (replicated via svn). That includes the bin directory containing CSharpInDepth.dll, which I've verified contains CSharpInDepth.Wave.RobotHandler.
I try to fetch http://csharpindepth.com/foo.robot and just get a 404.
I've tried with and without the assembly name, specific URLs or wildcarded ones... nothing's working.
I'm sure I've just missed some simple flag somewhere in the IIS configuration, but I'm blowed if I can find it...
EDIT: It's IIS version 6. Attempting to add *.robot to the ISAPI filter now...
Well if the hosting box is IIS7 in integrated pipeline you need to add it into the other bit of the config:
<system.webmodules>
....
<modules>
<add name="RobotHandler" type="CSharpInDepth.Wave.RobotHandler, CSharpInDepth"/>
</modules>
....
</system.webmodules>
If it's IIS6 then you'll need to map *.robots to the ASP.NET ISAPI DLL.
(For the non-Skeets you do this as follows)
Open up IIS admin.
Right click on
the Web site you want to configure
and select Properties form the
context menu. This will display the
Web Site Properties dialog.
Select
the Home Directory tab and click the
Configuration button. This will
display the Application
Configuration dialog box.
Click
Add.
Select the aspnet_isapi.dll
from the .NET framework directory,
the extension you want mapped and
either All Verbs, or just the ones
you want to map.
Click ok.
Jon,
You'll have to configure the IIS script mappings to pass *.robot to aspnet_isapi.dll.

Resources