I'm creating a custom server control which uploads files asynchronously to the server.
This solution uses flash element that posts the files to Generic Web handler aka ashx which then saves the posted file in a desired location.
It works grate, but with this approach I do need to create ashx file in each project, potentially, to handle the posts from the flash element.
What I would like to achieve is fully encapsulated server control that will use it's own ashx (or whatever can replace it) handler to upload the files.
Is it possible to do? Any ideas would be welcome.
Thanks.
You could include the handler within the class library containing the control and then only register it in web.config using the <httpHandlers> section:
<configuration>
<system.web>
<httpHandlers>
<add verb="*"
path="upload.ashx"
type="MyControl.MyUploadHandler.New, MyControl" />
</httpHandlers>
<system.web>
</configuration>
and if you are using IIS 7 Integrated pipeline mode to the <handlers> section:
<system.webServer>
<handlers>
<add name="UploadHandler"
verb="*"
path="upload.ashx"
type="MyControl.MyUploadHandler.New, MyControl" />
</handlers>
</system.webServer>
And if you are using ASP.NET 4.0 you could checkout the PreApplicationStartMethod infrastructure which allows you to dynamically register handlers:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyControl.StartUp), "PreApplicationStart")]
Related
I'm trying wire up a custom IHttpHandler to process request to my website for *.js files. For the life of me I can't get my httphandler to even be created by IISExpress when a request is made for a js file to my web server. I've tried several variations of the system.webServer handlers setting including.
<add name="MyHandler"
type="BusinessLayer.Web_Stuff.MyHandler, BusinessLayer"
verb="*"
path="*.js"
resourceType="Unspecified"
preCondition="integratedMode"
modules="IsapiModule"
scriptProcessor="c:\windows\Microsoft.net\framework\v4.0.30319\aspnet_isapi.dll"/>
and
<add name="JSHandler"
type="BusinessLayer.Web_Stuff.MyHandler, BusinessLayer"
verb="GET,POST" path="*.js"
resourceType="Unspecified"
preCondition="integratedMode" />
I am running this in Visual Studio 2013 using IISExpress. If i change the path to *.test instead of js then this handler works without any problems, but i cant make it work for *.js files. It seems like IISExpress is handling those requests itself by just serving the files off of disk. Does anyone know how to bypass that so i can make my handler process requests for js files.
Correction: When i use the top web.config setting with IsapiModule specified, an instance of MyHandler is instantiated however the ProcessRequest(HttpContext context) method of the interface is never called on my object. With the bottom definition, no instances of my handler are ever instantiated.
After fighting with this for several hours i finally found the answer. The key was I had to add an attribute to the modules xml element in system.webServer
<modules runAllManagedModulesForAllRequests="true" >
Without this attribute, the process request handler was not being called. After setting that, this is all that was need in the handlers section.
<add name="JSHandler"
type="BusinessLayer.Web_Stuff.ScriptMinifyHandler, BusinessLayer"
verb="*"
path="*.js"
resourceType="Unspecified"
preCondition="integratedMode" />
I will have to read a bit more about this setting, but if anyone can tell me more about why that works I'd love to hear it.
How do I configure IIS 7.5 to forward all *.xml file requests to asp.net engines so i can handle them in Global.asax and rewrite the path to a *.aspx file? Now IIS is expecting to find them directly on disk. I will use this do dynamically generate my sitemap.xml
You can force static files to go through the ASP.NET pipeline by editing your web.config:
<system.webServer>
<handlers>
<add name="XMLHandler" type="System.Web.StaticFileHandler" path="*.xml" verb="GET" />
</handlers>
</system.webServer>
HTTP Handlers and HTTP Modules Overview
How to: Register HTTP Handlers
i am pretty new to MVC and Routing and i was asked to modify an app to use diffrent url's.
a task that is a bit over me since i have no experience.
ok, lets talk a bit of code:
routes.MapRoute(
"CategoryBySeName", // Route name
"products/{SeName}", // URL with parameters
new { controller = "Catalog", action = "CategoryBySeName" }
);
this works as expected, but then the client wanted ".html" at the end of paths, so i changed:
"products/{SeName}", // URL with parameters
to:
"products/{SeName}.html", // URL with parameters
which fails ( IIS 404 page - MapRequestHandler)
it seems like iis is trying to load a physical file with that name instead of passing it to the application.
Similar: ASP.NET MVC Routing to start at html page (not answered, Not duplicate)
You have to force all request through the ASP.NET pipeline, and you can do that by adding only this single line to the web.config of your application:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
You're guess that an IIS handler is probably grabbing the request prior to MVC is likely correct.
Assuming IIS 7:
http://technet.microsoft.com/en-us/library/cc770990(v=ws.10).aspx
You need to edit the .html handler in IIS to use ASP.NET.
You can find it in the website properties under the home directory tab in app configuration in the mappings section in II6.
Something along the lines of (version may be different):
C:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll is what you need to handle the .html files.
Changing the Application Pool from Classic to Integrated fixed the issue.
thank you guyz for your help.
Just add this section to Web.config, and all requests to the route/{*pathInfo} will be handled by the specified handler, even when there are dots in pathInfo. (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189)
<location path="route">
<system.web>
<httpHandlers>
<add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</location>
What kind of purpose do .axd files serve?
I know that it is used in the ASP.Net AJAX Toolkit and its controls. I'd like to know more about it.
I tried Googling for it, but could not find getting basic information.
from Google
An .axd file is a HTTP Handler file. There are two types of .axd files.
ScriptResource.axd
WebResource.axd
These are files which are generated at runtime whenever you use ScriptManager in your Web app. This is being generated only once when you deploy it on the server.
Simply put the ScriptResource.AXD contains all of the clientside javascript routines for Ajax. Just because you include a scriptmanager that loads a script file it will never appear as a ScriptResource.AXD - instead it will be merely passed as the .js file you send if you reference a external script file. If you embed it in code then it may merely appear as part of the html as a tag and code but depending if you code according to how the ToolKit handles it - may or may not appear as as a ScriptResource.axd. ScriptResource.axd is only introduced with AJAX and you will never see it elsewhere
And ofcourse it is necessary
Those are not files (they don't exist on disk) - they are just names under which some HTTP handlers are registered.
Take a look at the web.config in .NET Framework's directory (e.g. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config):
<configuration>
<system.web>
<httpHandlers>
<add path="eurl.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="True" />
<add path="trace.axd" verb="*" type="System.Web.Handlers.TraceHandler" validate="True" />
<add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="True" />
<add verb="*" path="*_AppService.axd" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="False" />
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="False"/>
<add path="*.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="True" />
</httpHandlers>
</system.web>
<configuration>
You can register your own handlers with a whatever.axd name in your application's web.config. While you can bind your handlers to whatever names you like, .axd has the upside of working on IIS6 out of the box by default (IIS6 passes requests for *.axd to the ASP.NET runtime by default). Using an arbitrary path for the handler, like Document.pdf (or really anything except ASP.NET-specific extensions), requires more configuration work. In IIS7 in integrated pipeline mode this is no longer a problem, as all requests are processed by the ASP.NET stack.
An AXD file is a file used by ASP.NET applications for handling embedded resource requests. It contains instructions for retrieving embedded resources, such as images, JavaScript (.JS) files, and.CSS files. AXD files are used for injecting resources into the client-side webpage and access them on the server in a standard way.
Here's what I want to do:
I've created a class library project
and this has a class implementing
the IHttpHandler interface. Let's
call this class ZipHandler. Let's
say the namespace is Zip.
I want that whenever any Http
request comes for a zip file, my
ZipHandler should handle it,
regardless of whether the request is
to an Asp.Net application or a
normal virtual directory.
Queries:
Is it possible (it should be given
the hype about integrated pipeline
etc. in IIS 7)?
How to do it?
Here's the info I was looking for:
If you want to register your custom
HTTP handler at the IIS 7 Web server
level, you must compile your HTTP
handler into a strongly-named assembly
and deploy it to the Global Assembly
Cache (GAC) because IIS 7 only picks
up assemblies deployed to the GAC. It
does not pick up assemblies deployed
anywhere else such as the bin
directory of a particular Web site or
Web application.
We're aiming to add this handler at web server level. After deploying the handler in GAC, open the web.config available at the web server level (right click and browse -> open the web.config show here) and put something like this in the handler section (the fully qualified name of the class):
<handlers>
<add name="Ch8_RssHandler" path="*.rss" verb="*"
type="ProIIS7AspNetIntegProgCh8.RssHandler, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=369d834a77" preCondition="integratedMode" />
</handlers>
Note: The information snippets (1st para and code sample) are taken from the book:
Professional IIS 7 and ASP.Net Integrated Programming by Dr. Shahram Khosravi
Seems like a very nice book :)
This MSDN article How to: Configure an HTTP Handler Extension in IIS explains what you'll have to do. See the paragraph for the Integrated mode.
The file-name extension for .zeip must be registered in both the httpHandlers element and the handlers element.
You'll have to click Add Managed Handler in the Actions pane.
Using IIS Manager in IIS 7.0 to add a custom handler extension is equivalent to registering the handler extension in the Web.config file.
I did a test in VS2012
My handler is like this
namespace MyProject
{
public class ZipHandler: IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context) { ... }
}
}
My web.config is
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<handlers>
<add
name="ZipHandler"
path="*.zip"
verb="*"
type="MyProject.ZipHandler"
preCondition="integratedMode"
/>
</handlers>
</system.webServer>
</configuration>
In this way I can ask for "foo.zip" and have my handler get the request.
There's also a post from Rick Strahl that can help you troubleshoot things about handlers and modules: HttpModule and HttpHandler sections in IIS 7 web.config files