I have a web application which uses MVC4, but now realised it really should be a Web API and as such I'm moving over to a ASP.NET 4.5.2 web API 2 project.
The problem is, where I was using Request.ServerVariables[]
in my MVC project I have been advised that it's not the 'correct' way in a Web API, as such, I should be using Request.RequestUrl
The example on the MSDN is simply
Uri MyUrl = Request.UrlReferrer; //kaboom for me
Response.Write("Referrer URL Port: " + Server.HtmlEncode(MyUrl.Port.ToString()) + "<br>");
Response.Write("Referrer URL Protocol: " + Server.HtmlEncode(MyUrl.Scheme) + "<br>");
The issue is, despite referencing System.Web
there is no property for Request
are UrlReferrer
- the only properties are Content
, Headers
, Method
, Properties
, RequestUri
and Version
.
Why can't I use Request.UrlReferrer
as per the MSDN example?
I suspect you just need to get the current request in a different way, due to being in a static method rather than in a context where you can refer to a Request
instance property:
var request = HttpContext.Current;
var referrer = request.UrlReferrer;
...
See more on this question at Stackoverflow