Request.Islocal Feature added to the Asp.Net Web API
Request.Islocal feature added to the Asp.web API:
Asp.net introduced a good feature to the Asp.net webAPI. Frequently asp.net developers who are working local and remote environments used user defined methods to identify local requests and remote requests.So with Request.Islocal feature this functionality simplified
Asp.net introduced a good feature to the Asp.net webAPI. Frequently asp.net developers who are working local and remote environments used user defined methods to identify local requests and remote requests.So with Request.Islocal feature this functionality simplified
Request.isLocal in Web API
In the old HTTP model (System.Web.dll), Request.IsLocal returned true if the IP address of the request originator was localhost (or 127.0.0.1) or if the IP address of the request is the same as the server’s IP address.
While HttpRequestMessage indeed does not have IsLocal anymore, Web API will track this information in a Lazy<bool> inside the Request.Properties dictionary, under a rather vagueMS_IsLocal key.
With this information, we can now write a very simple extension method:
This now allows you to simply call isLocal() anywhere in the Web API pipeline where the framework gives you access to the incoming request.
In an ApiController:
In a handler:
In a filter:
Or even in a formatter:
By the way – contrary to pulling this information out of HttpContext, this approach works for bothweb-host and self-host.
All in all – a very simple 2-line extension method, but quite useful for development purposes.
Labels:
ASP.NET