OWIN Middleware: find if the request has a matched route - asp.net-web-api

We would like to write a piece of OWIN middleware that looks at the current request and see if it matches any mapped routes (we are using web.api, so we register routes with the httpRouteCollection.MapHttpRoute method.
If the current request does not match any known configured routes we would like to reject it with a custom 404 message.
Is it possible to determine from the OWIN context whether the current request matches one of the defined routes?

The short answer is, no, you cannot. OWIN middleware does not designed to do that. It is independent of the application.
If you want a custom 404 error page, you can add a setting in the web.config file to do this for you:
<configuration>
<system.webServer>
<httpErrors>
<error statusCode="404" path="/somedir/oops404.htm" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
</configuration>
Hope that helps.

Related

Laravel Azure App Service Delete Method Not Allowed

Hosting a laravel app in Azure's App Service and every time the web application makes DELETE requests, it throws up 405 Method Not Allowed and returns the message below:
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.
I've exhausted all options I could find in the internet and tried all stuff mentioned. Hope someone could help.
If your application uses .NET WebAPI, you may get a 405 Method Not Allowed error when you issue a PUT or DELETE request.
This error is caused because the WebDAV module intercepted the request
In your web.config, try to insert this code
<system.webServer>
<modules>
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>
</system.webServer>
And this issue duplicate of this question

Getting No 'Access-Control-Allow-Origin' when sending large JSON via AJAX

I'm working on a web page that calls a REST webservice via ajax to get and insert data.
The problem is that we need to send a base64 image in a JSON. You know, the base64 image is the imaged converted to that large text: base64/fjhd7879djkdadys7d9adsdkjasjdshk...
When we try with a 1 KB image, it works.
But with a bigger file(55kb), it doesn't.
So I assume it has something to do with the maxRequest, but the error says that is No 'Access-Control-Allow-Origin'. But we havent fount any way to configure it. Please help.
By default browsers block json requests from other domains other than the page unless the json request has the Access-Control-Allow-Origin header, so you'll need to add that header to your json requests on that service or use the same domain for both.
More info here: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
You can try setting the maxJsonLength to it's maximum value in the web.config file.
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483647"/>
</webServices>
</scripting>
</system.web.extensions>
I know this is an old post, but for anyone who still might be having this problem, I solved it by adding two settings to Web.config as described here: https://west-wind.com/webconnection/docs/_4lp0zgm9d.htm
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647"></requestLimits>
</requestFiltering>
</security>
<!--snip-->
</system.webServer>
and
<system.web>
<httpRuntime maxRequestLength="2147483647" />
<!--snip-->
</system.web>

httpErrors not redirecting to correct page with MVC 3

I am trying to set up http custom error pages in my MVC 3 web application. I have added the following to my web.config:
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteUrl" defaultPath="/Error/404">
<clear />
<error statusCode="500" path="/Error/404"
</httpErrors>
When a 500 error occurs on a request I expected this to redirect the user my.domainname.com/Error/404.
I have an Error controller which contains one action called error, I have mapped requests for 404 to this action.
When I force a 500 error (verified by chrome developer tools network tab) on the website I receive a blank page with the following message:
"The page cannot be displayed because an internal server error has occurred."
If I remove the httpErrors section from my web.config then I get the actual exception message. Therefore the httpErrors section I have in the web.config must be working, I just do not understand how to set it up correctly.
Here is a list of the attributes I have picked and why:
errorMode="Custom": So that I can see the custom error page and verify it is working before changing it to local only.
defaultResponseMode="ExecuteUrl": This should allow me to specify a server relative url as the path, which sounds like what I want as I want to hit a controller action.
defaultPath="/Error/404": The relative url I want requested if I do not specify one.
Edit:
I should add that the site is running on IIS 7.5. I also do not wish to use the tag as this is designed to work with IIS 6 and should be used for IIS 7+.
Try using the customErrors section in the web.config (inside system.web) instead.
http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx
e.g.
<customErrors mode="On">
<error statusCode="404" redirect="Error/404" />
<error statusCode="500" redirect="Error/500" />
</customErrors>
It should be like Controller/Action/Id. In your case Action is missing. Check following example and change it as per your action methods...
<system.web>
<customErrors mode="On" defaultRedirect="/Error/Index/" >
<error statusCode="401" redirect="/Error/NoAccess/" />
<error statusCode="404" redirect="/Error/NotFound/" />
</customErrors>
....
</system.web>

Registering PATCH HTTP verb in IIS 7/7.5

I want to implement the recently approved PATCH HTTP verb in a RESTful service implemented with ASP MVC 3. I have added the following settings in the web.config file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="PATCHVerbHandler" path="*" verb="PATCH" modules="ProtocolSupportModule" requireAccess="None" />
</handlers>
<security>
<requestFiltering>
<verbs>
<add verb="PATCH" allowed="true" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
</configuration>
The action method is decorated with the AcceptVerbs("PATCH") attribute.
The service works properly with the PATCH verb. The URL gets routed to the right action method and returns the proper data.
The strange issue is if I using a different URL that does not match any routes using the PATCH verb, IIS returns "200 OK" instead of "404 Not Found". All the standard verbs (GET, PUT, DELETE, POST, HEAD, OPTIONS) do not have this problem.
Do I need to register additional handlers for the PATCH verb or is it a routing issue? Any help is appreciated.
You don't actually need a custom handler to process HTTP requests made with the PATCH verb; instead, you may want to keep decorating your actions with the AcceptVerbs("PATCH") attribute while checking that the ASP.NET ISAPI is configured to handle any verb (it is the default), including PATCH.
If you have to handle this kind of requests using a custom module, by the way, please keep in mind that it is the responsibility of the handler itself to set the status code for each request (including the ones it should handle, according to the mapping, but it can't for whatever reason) and maybe it is not setting the correct value upon finishing.

Routing requests that end in ".cshtml" to a controller

(This is cross-posted to the ASP.NET forms)
I'm working on the WebGit .NET project, and we are close to a "1.0" release. However, I'm having trouble getting my "Browse" controller (which pulls files out of the repository) to serve-up ".cshtml" files.
I originally had trouble with ".config" and ".cs" files as well, but I fixed that with this in the web.config:
<location path="browse">
<system.webServer>
<security>
<requestFiltering>
<fileExtensions allowUnlisted="true">
<clear />
</fileExtensions>
<hiddenSegments>
<clear />
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
</location>
The routing that should be handling this request (that is successfully routing everything else) is:
routes.MapRoute(
"View Blob",
"browse/{repo}/blob/{object}/{*path}",
new { controller = "Browse", action = "ViewBlob", path = UrlParameter.Optional });
Now, whenever I try to access a URL that ends in ".cshtml", it gives a 404, even though my request should have been handled by the "Browse" controller. The files I'm serving-up do not exist on disk, but are instead pulled from a git repository as blobs. Every other file extension that I have tried works just fine.
How can I fix this behavior?
EDIT: I have tried disabling WebPages like so:
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
But that appears to have no effect.
As a quick workaround, you can put a temporary browse.cshtml file at your application root and put this inside your web.config,
add key="webpages:Enabled" value="false"
This is a known bug in ASP.NET WebPages, which gets implicitly loaded when you are using MVC 3. I don't think there is a straightforward way of disabling this behavior. The only workaround is to use a different extension (specifically, one that is not listed via WebPageHttpHandler.GetRegisteredExtensions())
This will be fixed in MVC 4, however. Sorry for the inconvenience.

Resources