ASP .NET code in SVG? - asp.net

I am looking for a way that I can add .SVG files to server-side code handlers or something, so that I can have server-side code handled in my .SVG files, without needing to change to the .ASPX file extension.
I know how to do this in PHP, just not in ASP .NET.

Implement an ASP.NET HTTP handler. See HTTP Handlers and HTTP Modules Overview.
Basically, you create an implementation of the System.Web.IHttpHandler interface and register it in web.config (you can register it for any URL pattern you like, so you can serve *.svg URLs and have the handler execute your logic).
If you're running an older IIS version, you will also need to add a file extension mapping to IIS configuration. For IIS 7, all you need to do to add the mapping is to register your handler in web.config.

Related

What is the diff between http handler and ashx

I know that httphandler is different from an ashx file. But I heard it from somewhere that they are the same. I am not able to find any reading material that shows that these two are different.
Basically, ashx is to HttpHandler, what apsx is to an ASP.NET (WebForms) Page.
It's just the default extension for an HttpHandler and there's nothing stopping you from using a different extension and configuring your server to use that extension.

Does ASP.NET not recognize an HttpModule if it doesn't have an accompanying .asmx file?

I created an HttpModule (without any associated axmx file), hooked it up via my web.config, and I'm able to track my incoming requests. A colleague commented that ASP.NET won't recognize it if there isn't any asmx file associated with it. My solution still works. Would anybody know any case where this won't work? For AJAX requests, maybe?
There is nothing wrong with what you have done.
Asmx is for web services.
Http Modules optionally can have an ashx file associated with it.
You have to link the class via web.config.
Only having a class is perfectly normal. You should not face any issues with it.
If you hooked the module in web.config you're fine. No need to register it elsewhere. There is just difference where to register it in web.config - for IIS7++ its in the system.webServer section, for IIS6 and lower it is in the system.web section.

IIS handle static files?

Does IIS handle request that of static file eg:
http://localhost:9000/Content/ABC.pdf
If it doesnt then can we add some setting so that the .pdf request is also handled by IIS and it passes through URLRewite module.
Asp.net only receives requests for aspx, asmx, ashx.
If a file name extension has not been mapped to ASP.NET, ASP.NET will not receive the request.
If you create a custom handler to service a particular file name extension, you must map the extension to ASP.NET in IIS and also register the handler in your application's Web.config file. For more information, see HTTP Handlers and HTTP Modules Overview.
If possible change your url to an ashx file. If not, you can map pdf to be recognized by asp.net.
Yes, IIS handles static content just fine (it does serve images up, right?).
By default it will bypass any dynamic processing and return the content directly.
If your setup does not automatically handle PDF files correctly, you may simply need to add the correct mime type to the configuration.

BeginRequest fires for static files in ASP.NET MVC app

I was under the impression that static files (CSS, images, #font-face files, etc) bypassed ASP.NET completely, and were served directly by IIS.
However, my BeginRequest event handler is being called for every HTTP request, including those for static files. This concerns me because I'm creating an Entity Framework data context to be used for the lifetime of each request in that event handler. I don't want to create those contexts if they're never going to be used.
I'm using IIS 7 on Windows 7 Ultimate with no special handler mappings defined. Do I have it wrong? Should these events be firing?
I believe a default ASP.NET MVC site has this set in the web.config.
<modules runAllManagedModulesForAllRequests="true" />
This means every .NET module will be loaded for every IIS request. This is required for ASP.NET MVC to handle extension-less routing. It's essentially a wildcard mapping that you would write in IIS that would match everything and route it to ASP.NET that lives in the web.config.
Read more here, including a way to disable the behavior if you aren't using .NET 4.0. It is nasty, but it's the cleanest solution for sites that can't deal with the overhead of having static files served by asp.net.
BeginRequest will be triggered for all requests (including static content) if:
You're using Visual Studio's development web server.
You've configured IIS to do so.
Please take a look at:
http://forums.asp.net/t/1220664.aspx
In addition to fixing the issue for your static files, you could use Lazy initialization Lazy<T> for your ObjectContext: http://msdn.microsoft.com/en-us/library/dd997286.aspx
The integrated mode in IIS 7 works differently than it was before.
You could switch to classic mode if desired.
Alternatively you can define your custom route handler and do the context initialization there. That way it's only done for specific routes.

Can an assembly in an ASP.NET Bin folder automatically register HTTP handlers?

Is it possible for a .NET assembly in an ASP.NET web site's Bin folder to automatically respond to certain incoming HTTP requests, as if it had an httpHandlers entry in the web site's web.config file, but without actually having to add that entry?
For example an assembly may contain the following metadata to get some embedded static resource to be available in an ordinary HTTP request:
[assembly: System.Web.UI.WebResource("SomeManifestResource", "image/gif")]
I'm looking for similar functionality that instead of returning a static resource will actually invoke an HttpHandler that is defined the assembly. Again, without actually having to add the entry to the web.config file.
No that is simply not possible.
Everything in ASP.NET that responds back with HTML output is HttpHandler somewhere in the inheritance hierarchy. So HttpHandlers are really one of the most critical part of ASP.NET runtime. Even your asp.net pages [aspx,ascx] do implement IhttpHandler to be able to serve HTML content.
You must configure them in your web.config to have them work for you.

Resources