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
)

asp.net C# Url.Action with routevalues

Using routevales you can change area and/or pass parameters on to the link you wish to hit.

//"normal" Url.Action - creates a link in the area where it is called from (and don't pass any values along)
<a href="@Url.Action("action", "controller")" class="myCssClass">link text</a>
//Url.Action with a that links to another area (area name or empty for the default area); also sets the category to shoes and it passes along the id that this view has.
<a href="@Url.Action("action", "controller", new { Area = "", id = Model.id, category="shoes"})" class="myCssClass">link text</a>