Site deployed, asp pages work fine, but not my handlers - asp.net

I have deployed a website using Visual Studio 2010 on IIS6. I used one of the four methods available : the basic file copy. It's like ordering to build to a different location, rather than the usual debug/release path in the project folder.
Anyway, the site that I released is responsive. As I have given all authorizations possible, I can browse contents, get files, most importantly execute asp/aspx pages.
I have declared the following http handler, it answers to URL/[anytext].text in debug mode (i.e. http://localhost/blablabla.text) and sends back an empty XML at the moment.
The same thing doesn't work after I deploy.
Code of my handler :
namespace WebApplication3
{
public class HttpHandler : IHttpHandler
{
public void ProcessRequest(System.Web.HttpContext context)
{
HttpResponse objResponse = context.Response;
objResponse.ContentType = "text/plain";
objResponse.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
objResponse.Write("</xml>");
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
web.config that gets deployed at the root of my IIS virtual directory :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation targetFramework="4.0" />
<httpHandlers>
<add verb="*" path="*.text" type="WebApplication3.HttpHandler, WebApplication3"/>
</httpHandlers>
</system.web>
</configuration>
And just if that can help, this is the web.config that I have in my VS2010 solution :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpHandlers>
<add verb="*" path="*.text" type="WebApplication3.HttpHandler, WebApplication3"/>
</httpHandlers>
</system.web>
</configuration>
Why would that not work as well right after the release ? I guess there's something missing. Most likely the web.config is never read ?

I finally found what this was about. The deployment process I described is fine. What you need to know, is that you need to tweak your IIS settings depending on what kind of handler your are adding.
In my case, I needed to add an extension (.text) in IIS.
http://msdn.microsoft.com/en-us/library/bb515343.aspx

Related

Api is giving 404 when added + as part of URL (%2B)

I am using asp.net core 3.1 and receiving values from URL. Its working fine but when I add "+" sign as a part of URL, it gives 404.
Example : localhost:9000/api/get/%2B12/values
+ is a special character. It should ideally be should be URL encoded as %2B.
Turns out it's not really required though (like in the screenshot below) but I still recommend it. (See: URL encoding the space character: + or %20?)
Here's a working example controller:
[ApiController]
public class ExpController : Controller
{
[Route("/api/exp/{arg}/values")]
public IActionResult Test(int arg) =>
Ok($"The arg is: {arg}");
}
Note how the route parameter is a int. For more complex values (other than just 12, +12, -12; eg: 12+12) the route will need to be a string instead.
version above IIS7 will refuse to request the URL contains symbols such as '+' by default. The following modifications are required. You need add the following nodes in web.config:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true"/>
</security>
</system.webServer>
But now the .net core project does not have web.config to configure IIS options. You need to go to the location:
vs project location /.vs/config/applicationhost.config to add the above node.
Note that the .vs folder is a hidden folder and needs to be set visible.
Option 1 :
Mess with config to bypass request validation / allowDoubleEscaping (Asp.Net)
You need to be aware for certain risk/vulnabilirities described here:
https://stackoverflow.com/a/53621095/4798459
.netcore :
Since this issues is related to IIS, not your solution. You need to handle a web.config
Create a new web.config on the root of your project.
Right click, properties, set "Copy to Output Directory" to "Copy Always"
When you publish a .net core app, a "basic web.config" file is created. (For iis)
You need to copy the content of the "basic web.config".
You can find the auto-generated web.config file:
Where your app is already published (local server?)
You can also publish your api temporarly to a random path on your PC, see details here https://docs.devexpress.com/OfficeFileAPI/401445/dotnet-core-support/publish-net-core-application)
The web.config should like so, i added the tag with a a commentt
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<!-- XML node <security> is added to allow allowDoubleEscaping and add support for + paremeter in a route. Risk:https://stackoverflow.com/a/53621095/4798459 -->
<security>
<requestFiltering allowDoubleEscaping="true"></requestFiltering>
</security>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments="[.\SolutionName.Namespace.dll]" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
Make sure that the step 2 is done before publishing your app otherwise it wont work.
Not tested with iisexpress
Option 2
Change pramater type in the api. Intead of being on the route, use a queryString instead
Option 3
Custom solution for request filtetring /routing, which i don't have any example, and seems a bit "over the top".
Option 4, to avoid:
Use an other solution for encoding / decoding special caracter (I have not tried)
https://stackoverflow.com/a/55637235/4798459

Receiving 'This operation requires IIS integrated pipeline mode.' error when adding httpModule to VS web project

Can someone shed some light on what's going on? I have a website created using VS 2010. The following code is added by default by VS when adding an httpModule. When I run the app through Casseni, the highlighted line throws the error "This operation requires IIS integrated pipeline mode."
public void Init(HttpApplication context)
{
// Below is an example of how you can handle LogRequest event and provide
// custom logging implementation for it
**context.LogRequest += new EventHandler(OnLogRequest);**
}
#endregion
public void OnLogRequest(Object source, EventArgs e)
{
//custom logging logic can go here
}
My web.config file was updated as such:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="GlobalModule" type="MyApp.Global.GlobalModule, EduCarePro"/>
</httpModules>
</system.web>
<system.webServer>
<modules>
<remove name="GlobalModule"/>
<add name="GlobalModule" type="MyApp.Global.GlobalModule, EduCarePro" preCondition="managedHandler"/>
</modules>
</system.webServer>
Is there something else that must be configured in the Web.Config to prevent this error??
The answer to this question is that Visual Studio's development web server does not support this functionality. You must run this code on an a machine running IIS.
TAKEN FROM (And Worked for me too as i had same problem)
Link

new handler triggers debug failure in visual studio

I have an existing asp.net website that works. When I (F5) debug it works. However, I am working on a new IHttpHandler for the site. As soon as I add the <system.webServer><handler></handler></system.webServer> section to the web.config visual studio refuses to F5 debug with the error:
Unable to start debugging on the web server. The web server could not
find the requested resource.
With the handler in place, if I attach-to-process then I can successfully attach to the process (and with the System.Diagnostics.Debugger.Break(); line I can step through the handler's code). I also added my handler to a different website and was able to reproduce this issue.
My Environment: .NET 4.0, Visual Studio 2012, using local IIS in integrated mode on Windows 7.
While trying to sanitize the code to paste here, I ended up commenting out everything in my handler except the boiler-plate, and the issue still occurs. Here are the code snippets:
The handler class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MySvc
{
public class MyServiceHandler : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
//#if DEBUG
// System.Diagnostics.Debugger.Break();
//#endif
}
}
}
and the web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<clear/>
<add key="A1" value="sanitized"/>
<add key="A2" value="sanitized"/>
</appSettings>
<connectionStrings>
<clear/>
<add name="MyDatabase" connectionString="sanitized"/>
</connectionStrings>
<system.web>
<compilation debug="true"/>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600"/>
<sessionState mode="SQLServer" cookieless="false" timeout="5" allowCustomSqlDatabase="true" cookieName="My.Session"
sqlConnectionString="sanitized" />
<machineKey
validationKey="sanitized"
decryptionKey="sanitized"
validation="sanitized" decryption="sanitized" />
</system.web>
<system.webServer>
<handlers>
<clear />
<add name="MyHandler" path="*.bwsvc" verb="*" type="MySvc.MyServiceHandler, MySvc" />
</handlers>
</system.webServer>
</configuration>
I've read through The Web Server Could Not Find the Requested Resource and many other articles along the same lines. None of it seems applicable to this situation, nothing mentions handlers causing problems.
Am I missing something in my handler or is this something Visual Studio doesn't support or some other issue?
I found the problem. It turns out the <clear /> in the <handlers> section was responsible for causing the problem. As soon as that line is removed, the debugger works again (and many other things too).
Since my application pool is in integrated pipeline mode, the handlers section actually inherits from the server settings which specify what handles such core things as *.aspx and such, so doing a clear on it means that IIS didn't know what to do with anything in my application.

VirtualPathProvider doesn't (quite) work in production on IIS 7.5

I have been working on a project that has common bits of functionality, specifically I wanted to share the master file and related images/js/etc. To that end, the master page and its dependent files are all wrapped into a "global" DLL that is utilized by all "subprojects". This all worked great in development, but deployment yielded a surprise which seems to catch a lot of people off guard: VirtualPathProvider doesn't work when precompiled.
Now thanks to this blog post containing a workaround I was able to give another attempt at getting it to work. Regretfully, it still doesn't.
I opted to get rid of my Global.asax implementation and went with the blog post's AppInitialize approach:
public static class AppStart
{
public static void AppInitialize()
{
HostingEnvironment hostingEnvironmentInstance = (HostingEnvironment)typeof(HostingEnvironment).InvokeMember("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
MethodInfo mi = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.NonPublic | BindingFlags.Static);
mi.Invoke(hostingEnvironmentInstance, new object[] { new MasterPageProvider() });
}
}
Since the actual provider works in debug, I won't include it. If you would like to see it, don't hesitate to ask. Just wanted to keep the question as short as possible.
The interesting aspect to this whole situation is that production yields no errors about not being able to find the master page. To me, this means the provider is working, but for whatever reason the rest of the resources (js/css/etc) aren't being retrieved from the assembly properly.
So my question comes down to this: what are the reasons that this solution would work great in development, but not in production on IIS 7.5?
UPDATE 11/20/2011
Tried out David Ebbo's suggestion and had no results unfortunately. My web config looks something like this now:
<configuration>
<connectionStrings>
<clear />
<!-- ... -->
</connectionStrings>
<system.web>
<pages>
<controls>
<!-- ... -->
</controls>
</pages>
<compilation debug="true" targetFramework="4.0" />
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
UPDATE 11/21/2011
Just to verify my suspicion that the VirtualPathProvider was actually working, I commented out the third line (mi.Invoke(....) and redeployed the site. As I suspected, it now breaks due to not being able to find the MasterPage file. This issue appears to be related to only static files being delivered through the VPP.
IIS 7.5 will handle the static files itself. You need to put a line for each static file you want it to ignore in your web.config file to make them get routed through your VPP. See below for examples.
<system.webServer>
<handlers>
<add name="Images" path="*.png" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
<add name="Stylesheets" path="*.css" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
</handlers>
</system.webServer>
Maybe the problem is that requests for static files are not going through ASP.NET by default in IIS.
Try whether turning on runAllManagedModulesForAllRequests in web.config helps. e.g.
<modules runAllManagedModulesForAllRequests="true" />
Take a look at this post. It explains how to get static files through a virtual path provider in IIS 7. I believe this will solve your problem.

Known Issues about having multiple web.config files and visual studio 2005/2008 SP1

I seem to be having a problem with my visual studio 2005/2008 installation or something because it isn't providing any IntelliSense whatsoever for controls registered on web.config files in folders different from the root, but it isn't showing any errors neither. Is this behavior normal?
I have access only to the folder of my sub-app, so I can't modify the root's web.config file. Well I COULD, but I'm NOT allowed to.
What I'm trying is to register some WebUserControl's on the web.config file for my sub-app folder, so all the pages in my sub-app can use the WebUserControl's without having to register them on every page, but I'm not getting IntelliSense for those controls registered on the web.config file on my sub-app folder, but I do get IntelliSense if I register them on the root's web.config file. IntelliSense for everything else appears to be working fine.
In the web.config file on my sub-app folder I have something like the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="qme" tagName="EmptySearchMessage" src="~/QMinerals/WebUserControls/EmptySearchMessage.ascx"/>
<add tagPrefix="uc1" tagName="uc_general" src="~/uc_general.ascx" />
<add tagPrefix="uc1" tagName="uc_menu" src="~/uc_menu.ascx" />
</controls>
</pages>
</system.web>
</configuration>
am I doing something wrong?
Update
Now I have upgraded to VS2008 SP1 and the issue persist
Did you try removing "~" from the path and using it relative to web.config? (yeah, even if works now)
Do you have Visual Studio 2005 Service Pack 1?

Resources