I am trying to convert a user control to a helper method in Asp.NET MVC. However I do not seem to be able to have access to Request.Url.Host how do I get the correct host.
+3
A:
HtmlHelper has a ViewContext property. The ViewContext property has an HttpContext property. You should be able to access the Request from there.
public static string MyHelper( this HtmlHelper helper, ... )
{
var host = helper.ViewContext.HttpContext.Request.Url.Host;
...
}
Update: you might also be interested in some code I wrote to mock out the HtmlHelper class for my extension tests. You can find it on my blog: http://farm-fresh-code.blogspot.com.
tvanfosson
2009-10-01 15:21:39