不确定我是否真的清楚地理解了你的问题,但是,让我尝试一下。
要创建像您描述的那样的 HtmlHelper 扩展,请尝试以下操作:
using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace Something {
public static class PageLinkHelper
{
public static string PageLink(
this HtmlHelper helper,
string linkText, string actionName,
string controllerName, object routeValues,
object htmlAttributes)
{
return helper.ActionLink(
linkText, actionName, controllerName,
routeValues, htmlAttributes);
}
}
}
至于从类获取 URL 的问题,取决于您将实现它的类类型。例如,如果您想从 HtmlHelper 扩展获取当前控制器和操作,您可以使用:
string currentControllerName = (string)helper.ViewContext
.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext
.RouteData.Values["action"];
如果您想从控制器获取它,您可以使用基类 (Controller) 中的属性/方法来构建 URL。例如:
var url = new UrlHelper(this.ControllerContext.RequestContext);
url.Action(an_action_name, route_values);