How can I add a hook to T4MVC so that when I get a link to an action using T4MVC it checks if the current request has a parameter, lets say PIN=1234. If it exists, i want this parameter to be added to the generated link.
Asked
Active
Viewed 63 times
1 Answers
0
I created an extension method for UrlHelper that wraps the T4MVC's extension method for UrlHelper. This adds the required additional parameters to every link generated from T4MVC.
public static class T4MVCHelpers
{
/// <summary>
/// Returns a T4MVC generated url, passing along PIN
/// </summary>
public static string ActionUrl(this UrlHelper urlHelper, ActionResult result)
{
var pin = urlHelper.RequestContext.HttpContext.Request["Pin"];
if (!string.IsNullOrEmpty(pin))
{
result.AddRouteValue("Pin", pin);
}
return urlHelper.Action(result);
}
}
I then always call this wrapper method instead of T4MVC's method
var Url.ActionUrl(MVC.SomeController.Index(1));
Now depending on the requesting url. If the requesting url has no pin parameter it will look like this
/SomeController/Index?param1=1
Now depending on the requesting url. If the requesting url has PIN=123 it will look like this
/SomeController/Index?param1=1&PIN=123

Shrage Smilowitz
- 24,494
- 2
- 28
- 32