Unable to map an HttpHandler to a "path/*" wildcard mapping - asp.net

So I've been trying to map an http module to a sub-path of an MVC3 site. It should be pretty simple as I understand it, but it has not been working. The module is setup like so:
<handlers>
<add name="Nancy" path="api/*" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
</handlers>
A matching section is also there for iis6 so I can run it under webdev.webserver. However testing both deploying to my local iis7 (under Win7) and with webdev.webserver, only /api actually calls the handler. If I call /api/{anything} it just returns a 404.
I'm sure I'm just "doing it wrong (tm)" but any help would be appreciated.
Note: I've also tried a couple other configurations including using a tag and creating a /api folder and adding a web.config to that folder with a full wildcard.

Simple. Just put the path, no wildcard.
<handlers>
<add name="Nancy" path="api" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
</handlers>
This will match:
/api/{anything}

The URLRoutingModule-4.0 is a catch all handler listed before your nancy handler. It will thus come into play before your handler is ever hit. You can remove the handlers add yours and add them back in like so:
<handlers>
<remove name="BlockViewHandler" />
<remove name="UrlRoutingModule-4.0" />
<add verb="*" path="robots.txt" name="robots" type="System.Web.StaticFileHandler"/>
... custom handlers here
<add name="Nancy" path="api/*" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
... now add back UrlRoutingModule and BlockViewHandler
<add path="*" verb="*" name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" preCondition="managedHandler" />
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
You can see the handler order in IIS7 under Handler Mappings select View Ordered List and it will list the order in which it loads the handlers top (first) to bottom (last).
You might need a second Web.config in your /api folder
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<clear />
<add name="Nancy" path="*" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" allowPathInfo="true" />
</httpHandlers>
</system.web>
</configuration>
Similarly, this is what I usually do for "/static" content on websites. I have not found out how to circumvent the need for the seconds web.config.
EDIT
I had a hard time figuring this out when i had to as well and it seems my memory hasnt served me well. I dont specify a path/* handler anywhere instead I have this:
(only specifying simple wildcards/fully qualified paths to go around UrlRouting)
<location path="." inheritInChildApplications="false">
<system.webServer>
<!--
ml: in .NET 4.0 its now safe to remove from the modules section.
Make sure you have a *. mapping to a ExtensionLessUrl hanlder in IIS
this should improve performance a tad albeit neglectable.
see: http://blogs.msdn.com/b/tmarq/archive/2010/04/01/asp-net-4-0-enables-routing-of-extensionless-urls-without-impacting-static-requests.aspx
-->
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="false" />
<handlers>
<remove name="BlockViewHandler" />
<remove name="UrlRoutingModule-4.0" />
<add verb="*" path="robots.txt" name="robots" type="System.Web.StaticFileHandler"/>
.. Some company handlers i can't list
<add path="*" verb="*" name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" preCondition="managedHandler" />
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</location>
Then in my /Content/web.config file I set the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<clear />
<add name="StaticFiles" path="*" verb="*" modules="StaticFileModule" resourceType="Either" requireAccess="None" />
</handlers>
<staticContent>
<clientCache cacheControlMaxAge ="31.00:00:00" cacheControlMode="UseMaxAge" />
</staticContent>
</system.webServer>
</configuration>
My handler list for /Content/ looks like this now:
Which is about as sure as I can be anything in /Content/ will be served through StaticFileModule. The trick here seems to be specifying: inheritInChildApplications="false".

Seems the UrlRoutingModule-4.0 is more trouble than it is worth. Instead I've just told MVC3 to ignore the routes. Not a perfect solution but until I have something that works better I'll have to stick with this in RegisterRoutes:
routes.IgnoreRoute("api/{*route}");

Related

IIS bundles/less cache issue

I have an ASP.NET MVC 5 project that uses dotless for parsing/compiling .less files and standard bundler for minifying/bundling js files.
The issue is that on production server cache-conrole header is set to max-age=0, no-cache, no-store when locally on IIS express it's set to public and expires in a year.
Dotless config: <dotless minifyCss="true" cache="true" web="true" handleWebCompression="false" />
Bundle config - just includes and BundleTable.EnableOptimizations = true;
Few web config sections that may help:
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
...
<modules runAllManagedModulesForAllRequests="true">
<add name="Prerender" type="Prerender.io.PrerenderModule, Prerender.io, Version=1.0.0.2, Culture=neutral, PublicKeyToken=null"/>
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web" />
<add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate32" />
</modules>
...
<handlers>
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="dotless" path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler,dotless.Core" resourceType="File" preCondition="" />
</handlers>
I tried to add mime maps to staticContent section. Also thought about adding request headers in iis, but it works only for files/folders so it likely will not serve bundle.
What have I missed in configs? I appreciate any idea for fixing the issue.
Check if you got in your web.config that stuff:
<configuration>
<system.web>
<compilation debug="false"/>

Do credentials in web.config allow access to one user at time only?

I have a very small website that is used by a couple of users to share the same data in a table that they fill in. The plan is to sort this out properly very soon but for now is like that.
I am just comparing the user and password input with some keys in web.config:
<configuration>
<connectionStrings>
<add name="PMIcommConnectionString" connectionString="xxxxxx />
</connectionStrings>
<appSettings>
<add key="UserName" value="xxxxxx" />
<add key="Password" value="xxxxxxx" />
<add key="Telerik.Skin" value="Windows7" />
</appSettings>
<system.web>
<customErrors mode="On" defaultRedirect="~/Error.aspx" />
<compilation debug="true" targetFramework="4.5">
<assemblies>
.........
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
<pages>
<controls>
<add tagPrefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI" />
</controls>
</pages>
<httpHandlers>
........
</httpHandlers>
<httpModules>
<add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule" />
<add name="RadCompression" type="Telerik.Web.UI.RadCompression" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="RadUploadModule" />
<add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule" preCondition="integratedMode" />
<remove name="RadCompression" />
<add name="RadCompression" type="Telerik.Web.UI.RadCompression" preCondition="integratedMode" />
</modules>
<handlers>
......
</handlers>
</system.webServer>
</configuration>
My question is: can such login system be used to log on two users simultaeously?
Yes it can and every user will have its own session, but in turn it depends on how you implemented server side logic of user authentication. Is there forms authentication used?
Yes, it can, you'd want to have a way of linking them together though.
<add key="JON-Username" value="JON" />
<add key="JON-Password" value="jonspassword" />
<add key="MMC-Username" value="MMC" />
<add key="MMC-Password" value="mccspassword" />
Obviously, you'd need to write your server-side logic so that it knows how to get the proper key for the username.
Alternative, you could do something like this...
<add key="JON" value="jonspassword" />
<add key="MMC" value="mmcspassword" />
But the downside of that is that if you want to use any other AppSettings, you don't be able to tell which is intended for other settings and which is intended for credentials, which is why I recommend the first method.
That being said, my top recommendation is to use a proper database for storing this type of information, and provide an administrative view within the site for configuring who has access.
Ok, probably you can use this system to handle access to 'private' areas of your website, and yes, you can easily retrieve your key in appsetting by:
string jonUsername = WebConfigurationManager.AppSettings["JON-Username"];
string jonPassw = WebConfigurationManager.AppSettings["JON-Password"];
if(myUsernameInputfield.Text == jonUsername && myPasswInputfield.Text == jonPassw)
{
// ok this guy have the right to access to my page
}
to compare values inserted by your users
If you would do this, maybe you should you also encrypt your webconfig

RAD Editor Telerik control image Manager, and asp.net button issues (Not working together)

In writing and in telling its strange thing is happening with me. Let me tel you scene from first, i am using RAD editor with following code
<telerik:radeditor ID="RadEditor1" runat="server" Width="550px" ToolsFile="Tool.xml" skin="WebBlue" height="400" BackColor="LightGray" tabindex="6">
<ImageManager ViewPaths="~/Images/" UploadPaths="~/Images/" DeletePaths="~/Images/" />
<MediaManager ViewPaths="~/Images/" UploadPaths="~/Images/" DeletePaths="~/Images/" />
<FlashManager ViewPaths="~/Images/" UploadPaths="~/Images/" DeletePaths="~/Images/" />
<DocumentManager ViewPaths="~/Images/" UploadPaths="~/Images/" DeletePaths="~/Images/" />
</telerik:radeditor>
now with this i am not able to upload image with this image manager, so i got solutions from stackoverflow to add some script in web.confing as it is bellow, i got solution to upload image with image manager. but some how my buttons of site stops to work.
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="RadCompression" type="Telerik.Web.UI.RadCompression"/>
<add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule, Telerik.Web.UI" preCondition="managedHandler"/>
</modules>
<handlers>
<add name="Telerik.Web.UI.SpellCheckHandler.axd_*" path="Telerik.Web.UI.SpellCheckHandler.axd" verb="*" type="Telerik.Web.UI.SpellCheckHandler, Telerik.Web.UI, Culture=neutral, PublicKeyToken=121fae78165ba3d4" preCondition="integratedMode"/>
<add name="Telerik.Web.UI.DialogHandler.aspx_*" path="Telerik.Web.UI.DialogHandler.aspx" verb="*" type="Telerik.Web.UI.DialogHandler, Telerik.Web.UI, Culture=neutral, PublicKeyToken=121fae78165ba3d4" preCondition="integratedMode"/> <add name="Telerik.RadUploadProgressHandler.ashx_*" path="Telerik.RadUploadProgressHandler.ashx" verb="*" type="Telerik.Web.UI.Upload.RadUploadProgressHandler, Telerik.Web.UI"preCondition="integratedMode"/>
<add name="Telerik.Web.UI.WebResource.axd_*" path="Telerik.Web.UI.WebResource.axd" verb="*" type="Telerik.Web.UI.WebResource, Telerik.Web.UI" preCondition="integratedMode"/></handlers>
</system.webServer>
so please get me idea how this can be happen and what i have to do for this or any alternative solution to get work Image Manager (FYI: with above code i am able to upload image but asp.net button controls stop working)
Some of your handlers look a bit off to me. Here are the handlers I have in my web.config for some Telerik controls (I don't use their files managers, I programmed my own for custom functionality).
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
<handlers>
<add name="Telerik_Web_UI_DialogHandler_aspx" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.DialogHandler.aspx" type="Telerik.Web.UI.DialogHandler" />
<add name="Telerik_Web_UI_WebResource_axd" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource"/>
</handlers>
The fact that some of yours are ending with _* doesn't look right.

IIS - Using HttpHandler for all files except for one

I use a custom HttpHandler in .NET for all *.html files, however I want to create a single exception for the file foo.html, which should be handled as a static file on the hard drive. I've tried:
<httpHandlers>
<remove verb="*" path="*.html" />
<add verb="*" path="/foo.html" type="System.Web.StaticFileHandler" />
<add verb="*" path="*.html" validate="false" type="Imp.Handler" />
</httpHandlers>
As well as:
<httpHandlers>
<remove verb="*" path="*.html" />
<add verb="*" path="*.html" validate="false" type="Imp.Handler" />
<add verb="*" path="/foo.html" type="System.Web.StaticFileHandler" />
</httpHandlers>
However, both will cause Imp.Handler to handle foo.html requests still. What am I doing wrong?
IIS is running in Classic mode.
may be this method can help you,move the files to the other folder and disable httphandler for this folder.
<location allowOverride="false" path="Static">
<system.web>
<httpHandlers>
<remove verb="*" path="*.htm"/>
<!--<add verb="*" path="/foo.htm" type="System.Web.StaticFileHandler" />-->
</httpHandlers>
</system.web>
</location>
<httpHandlers>
<add verb="*" path="*.html" validate="false" type="Imp.Handler" />
</httpHandlers>
i trying test this,the question is :
if i remove HttpHandler for the "*.html" in the location element,and add a other httphandler will doesn't worked.for example:
<location>
<remove verb="*" path="*.htm"/>
<add verb="*" path="foo.html" validate="false" type="Imp.Handler" />
</location>

How do I restrict access to files with specific extensions in ASP.NET?

I have in my web application an ADO.NET Entity-Framework *.edmx file.
When I browse in the browser (when the application is running) to an edmx file, it doesn't show the error page like when browsing to a *.cs or vb file, it opens the edmx and shows my model scheme to all the users!!!
How can I avoid that.
You should map the extension to the ASP.NET's System.Web.HttpForbiddenHandler class in web.config. If you are using IIS6, before you could do that, you should have mapped the extension to ASP.NET ISAPI handler.
IIS7 Integrated Mode:
<system.webServer>
<handlers>
<add name="MyForbiddenExtensionHandler"
path="*.edmx"
verb="*"
type="System.Web.HttpForbiddenHandler"
preCondition="integratedMode" />
</handlers>
</system.webServer>
IIS7 Classic Mode. Something like:
<system.web>
<httpHandlers>
<add path="*.edmx"
verb="*"
type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</httpHandlers>
</system.web>
<system.webServer>
<handlers>
<add name="MyExtensionISAPI"
path="*.edmx"
verb="*"
modules="IsapiModule"
scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv2.0,bitness64" />
</handlers>
</system.webServer>
IIS6 (after mapping the handler to aspnet_isapi.dll in IIS6 configuration):
<system.web>
<httpHandlers>
<add path="*.edmx"
verb="*"
type="System.Web.HttpForbiddenHandler, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</httpHandlers>
</system.web>
You can do this two ways; firstly in the web.config or secondly in IIS
<system.web>
<httpHandlers>
<add verb="*" path="*.edmx" type="System.Web.HttpForbiddenHandler" />
</httpHandlers>
</system.web>
Here's a link to a microsoft support page that details how to do it in the web config and IIS.
http://support.microsoft.com/kb/815152

Resources