ASP.NET HTTP Handler Unrecognized Request - c#

I have ASP.NET handler. But when I try to call it it says :
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
namespace SimpleHTTPHanlder
{
public class SimpleHandler : IHttpHandler
{
#region IHttpHandler Members
bool IHttpHandler.IsReusable
{
get { return true; }
}
void IHttpHandler.ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
response.Write("<html><body><h1>Wow.. We created our first handler");
response.Write("</h1></body></html>");
}
#endregion
}
}
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpHandlers>
<add verb="*" path="vishal.nayan" type="SimpleHTTPHanlder.SimpleHandler"/>
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>
I try to make request like this, but with unsuccess:
http://localhost:60223/SimpleHTTPHanlder/vishal.nayan

Looking at our code which works, some ideas:
Try adding the handler under system.webServer instead of system.web
Try adding the attribute preCondition="integratedMode"
Try specifying a name attribute

I think as you have written your path as
http://localhost:60223/SimpleHTTPHanlder/vishal.nayan.
Instead of this try
http://localhost:60223/vishal.nayan
This is because your path element contain vishal.nayan only.
<httpHandlers>
<add verb="*" path="vishal.nayan" type="SimpleHTTPHanlder.SimpleHandler"/>
</httpHandlers>
if you still have issue then tell me does you have hosted on IIS or IIS express ?
If you have configured in IIS ( IIS 7 or 7.5 later then you have to configure in
<system.webServer>
<handlers>
<add name="test" verb="*" path="vishal.nayan" type="SimpleHTTPHanlder.SimpleHandler"/>
</handlers>
...... other configuration
</system.webServer>

http://localhost:60223/SimpleHTTPHanlder/vishal.nayan
is it a copy/paste with a typo ? ( Hanlder )

Related

ServiceStack api/auth/credentials returns 404 on frontend migration

I'm changing my frontend to React, so I started a new project with VS2015. I already configured ServiceStack with my old API services. Everything works fine except by one: api/auth/credentials.
I get a 404 when I try to send a POST.
My Configure has:
public MyApphost() : base("My web services", typeof(MyApphost).Assembly) {}
and:
(In a previous call to Plugins)
IoC.Container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(Dvtrck.Configuration.Manager.Current.Dsn, Dvtrck.Services.OrmLiteDialects.PostgreSQLDialectProviderLowerCase.Instance));
... (Some code and)
Plugins.Add(new AuthFeature(() =>
new UserSession(),
new IAuthProvider[] {
new CustomAuthProvider(), // CustomAuthProvider : CredentialsAuthProvider
}));
And my web.config has:
<location path="api">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
</system.web>
<!-- ServiceStack: Required for IIS7 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
</location>
I already checked similar questions but my case is different. Even I had no errors when adds AuthFeature. I don't get why works fine in my old project.
You also need to configure:
SetConfig(new HostConfig { HandlerFactoryPath = "api" });

Servicestack SOAP - Invalid credentials returning html in response

In my service, I have a custom auth provider that throws a HttpError if the credentials are invalid like so:
throw HttpError.Unauthorized("Invalid Username or Password");
When I access this service via REST and purposely enter invalid credentials, I get the expected response:
{
"ResponseStatus": {
"ErrorCode": "Unauthorized",
"Message": "Invalid UserName or Password",
}
}
However, doing the same thing via a SOAP client, I get a Html response from the IIS server:
This causes my SOAP client to break as it can't deserialize the response. This can also occur if incorrect data values are included in the request e.g. setting a string value on an integer parameter. The response is fine if an exception occurs in one of my services where I am handling the request (i.e. HttpError.NotFound thrown). I know a similar question has been answered here but it hasn't been updated since 2013. Is there a way to configure the response or override it?
Edit 1
I've updated my web.config to look like so (in system.webServer, I had to use "modules" instead of "httpModules".):
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<httpModules>
<add name="FormsAuthenticationDisposition" type="ServiceStack.SuppressFormsAuthenticationRedirectModule, ServiceStack" />
</httpModules>
<httpHandlers>
<add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<add name="FormsAuthenticationDisposition" type="ServiceStack.SuppressFormsAuthenticationRedirectModule, ServiceStack" />
</modules>
<urlCompression doStaticCompression="true" doDynamicCompression="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
and my AppHost has the following:
SetConfig(new HostConfig
{
HandlerFactoryPath = "/api"
});
SuppressFormsAuthenticationRedirectModule.PathToSupress = Config.HandlerFactoryPath;
However I'm still getting the same error as before. I'm wondering if this is due to me passing in credentials as a header with every request rather than specifically authenticating first? It's worth pointing out that this same error occurs when e.g. a string is set on an integer parameter.
Edit 2
This solution does in fact work. I was defining the incorrect HandlerFactoryPath:
SetConfig(new HostConfig
{
HandlerFactoryPath = "api"
});
The Yellow Screen Of Death is the result of ASP.NET Hijacking the Error Response of Unauthorized Error Responses. You can prevent ASP.NET from hijacking the Error Response with the SuppressFormsAuthenticationRedirectModule.
First you need to register the HTTP Module in your web.config:
<system.web>
<httpModules>
<add name="FormsAuthenticationDisposition" type="ServiceStack.SuppressFormsAuthenticationRedirectModule, ServiceStack" />
</httpModules>
</system.web>
<!-- Required for IIS 7.0 (and above?) -->
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<httpModules>
<add name="FormsAuthenticationDisposition" type="ServiceStack.SuppressFormsAuthenticationRedirectModule, ServiceStack" />
</httpModules>
</system.webServer>
next, configure the module with where your API lives - defaults to /api, so in your AppHost Configure:
public override void Configure(Funq.Container container)
{
SetConfig(new HostConfig {
HandlerFactoryPath = "api",
});
//this is the configuration for Hijacking prevention
SuppressFormsAuthenticationRedirectModule.PathToSupress = Config.HandlerFactoryPath;
}

Merging MVC in Webforms produces slow performance and redirects to login page in production

For an undefined reason when I tried to integrate MVC5 in a webforms app it works fine after adding routes ignores in global.asax on my machine, but in production seems the Session is null because it redirects to login.aspx after getting 302 from default.aspx.
Obvious issue:
Slow response.(~ 11.84 s)
Problem of Session.
public static void RegisterRoutes(RouteCollection routes)
{
//ignore aspx pages (web forms take care of these)
//routes.Add(new Route("favicon.ico", new StopRoutingHandler()));
routes.MapPageRoute("default", "", "~/default.aspx");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{folder}/{*pathInfo}", new { folder = "Services" });
//routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
// Route name
"home",
// URL with parameters
"{controller}/{action}/{id}",
// Parameter defaults
new { controller = "home", action = "index", id = "" }
);
}
Web.config:
<system.webServer>
<staticContent>
<remove fileExtension=".woff"/>
<remove fileExtension=".svg"/>
<mimeMap fileExtension=".woff" mimeType="application/x-font-woff"/>
<mimeMap fileExtension=".svg" mimeType="image/svg+xml"/>
</staticContent>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
</modules>
The problem is probably in you web.config file, check if it contains the following in you production environment.
<configuration>
...
<system.webServer>
...
<modules>
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
...
</modules>
</system.webServer>
</configuration>
A late response, but I'm going to post it anyway. The setting:
runAllManagedModulesForAllRequests="true"
Can have big impact in performance, e.g. on the time that images have to load. In your browser you can analyse the network activity and compare times of image and script loads with this setting set to true and false.

Custom handler failing to work due to Request Filtering

This is in IIS express.
public class FooHandler : HttpTaskAsyncHandler
{
public override async Task ProcessRequestAsync(HttpContext context)
{
var val = await new FooRequest().ProcessRequest();
context.Response.Write(val);
}
}
That s the handler and below is web.config.
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<sessionState mode="Off" />
<compilation debug="true" targetFramework="4.5" />
<machineKey compatibilityMode="Framework45" />
<httpHandlers>
<add verb="*" path="*.foo" validate="false" type="FooServer.FooHandler" />
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="FooHandler" verb="*" path="*.foo" type="FooServer.FooHandler" />
</handlers>
</system.webServer>
</configuration>
I keep getting the below error. google didnt help. may be you can?
HTTP Error 404.7 - Not Found
The request filtering module is configured to deny the file extension.
Most likely causes:
Request filtering is configured for the Web server and the file extension for this request is explicitly denied.

How do I exclude things that match the specified path for an HttpHandler in ASP.Net?

I know that if I want to have requests for MyPage.aspx go to the class called MyHandler in the assembly called MyAssembly, I can add this to my web.config file:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="MyPage.aspx" type="MyHandler, MyAssembly"/>
</system.web>
</configuration>
This works for any MyPage.aspx at the (made up) URL: www.mycoolsite.com/MyProject/[SomePathHere]/MyPage.aspx
What if I want to do it for every MyPage.aspx except www.mycoolsite.com/MyProject/NoHandler/MyPage.aspx
Is there a way to exclude that path from the handler?
You can put a web.config in the NoHandler folder that defines a different handler (NotFound if you want to server a 404 style, etc). Same format as your current web.config, just put only the elements you want to override like the handler.
Here's an example if you want to override with a 404 in that directory:
<configuration>
<system.web>
<httpHandlers>
<remove verb="*" path="MyPage.aspx" type="MyHandler, MyAssembly"/>
<add verb="*" path="MyPage.aspx" type="MySpecialHandler, MyAssembly"/>
</httpHandlers>
</system.web>
</configuration>

Categories