MVC3 check what url was called (regardless of server/interface)

In some cases there is a need to check if the url that is used to access a page corresponds to what we expect.

This can be done like this

if ( Request.RawUrl.ToString() == Url.Action("LogOut", "Account") ){
    //The url of the action was used to access this page (and no extra GET parameters or anything hinkey) - and any servername/ip we listen to was used
}

This differs from getting the URI in the way that this is the RawUrl is only the part after the server (so for http://example.com/url/to/stuffs RawUrl would contain /url/to/stuffs where the URI would contain details about how the user got there.

system.web.httprequest.rawurl at msdn

C# MVC# create a URI from a Url.Action

Sometimes the need arises to create a URI object instead of just a link. This can be done by using Url.Action.

new Uri(Url.Action("action", "controller", null, Request.Url.Scheme))
 
//definitions 
public Uri(
	string uriString
)
 
public string Action(
	string actionName,
	string controllerName,
	Object routeValues,
	string protocol
)

MVC3 Getting the URI in a controller

If there is a need to get the URI that the was used to reach the controller then the Request holds that information and it can be retrieved to a URI object

public ActionResult MyAction()
{
    var uri = Request.Url;
    var port = uri.Port;
    var absoluteUri = uri.AbsoluteUri
 
    //... do things here
 
    return View();    
}