Генерацию строго типизированных URL-адресов на основе делегатов в ASP.NET MVC
Но ни один из них на самом деле не делает то, что мне хотелось бы. В настоящее время у меня есть гибридный подход, например:
Код: Выделить всё
// shortened for Brevity
public static Exts
{
public string Action(this UrlHelper url,
Expression expression)
where T : ControllerBase
{
return Exts.Action(url, expression, null);
}
public string Action(this UrlHelper url,
Expression expression,
object routeValues)
where T : ControllerBase
{
string controller;
string action;
// extension method
expression.GetControllerAndAction(out controller, out action);
var result = url.Action(action, controller, routeValues);
return result;
}
}
Код: Выделить всё
public class MyController : Controller
{
public ActionResult MyMethod()
{
return null;
}
public ActionResult MyMethod2(int id)
{
return null;
}
}
Код: Выделить всё
Url.Action(c => c.MyMethod())
Код: Выделить всё
Url.Action(c => c.MyMethod2(-1), new { id = 99 })
Код: Выделить всё
Url.Action(c => c.MyMethod2, new { id = 99 })
Подробнее здесь: https://stackoverflow.com/questions/343 ... url-action
Мобильная версия