Asp.net mvc 创建自定义ActionLink

Asp.net mvc 创建自定义ActionLink,asp.net-mvc,asp.net-mvc-4,html-helper,actionlink,Asp.net Mvc,Asp.net Mvc 4,Html Helper,Actionlink,我想创建一个自定义actionlink,但我不知道如何在满足需求的同时做到这一点。我以前使用过定制的HTMLHelper,但这对我来说有点棘手 我要调用的actionlink需要如下: @Html.CustomActionLink("LinkText", "Area","Controller","TabMenu","Action",routeValues, htmlAttributes) @Html.CustomActionLink("Click here","Travel","Trip",

我想创建一个自定义actionlink,但我不知道如何在满足需求的同时做到这一点。我以前使用过定制的HTMLHelper,但这对我来说有点棘手

我要调用的
actionlink
需要如下:

@Html.CustomActionLink("LinkText", "Area","Controller","TabMenu","Action",routeValues, htmlAttributes)
 @Html.CustomActionLink("Click here","Travel","Trip","Index","Index", new { par1 = "test", par2 = test2, new { @class = "font-color-blue" })`
<a class="font-color-blue" href="/Trip/Travel/Index/Index?par1=test&par2=test2">Click Here</a>
 context.MapRoute(
            "EPloeg_default",
            "EPloeg/{controller}/{tabmenu}/{action}/{id}/{actionMethod}",
            new { action = "Index", id = UrlParameter.Optional, actionMethod = UrlParameter.Optional }
        );   
例如:

@Html.CustomActionLink("LinkText", "Area","Controller","TabMenu","Action",routeValues, htmlAttributes)
 @Html.CustomActionLink("Click here","Travel","Trip","Index","Index", new { par1 = "test", par2 = test2, new { @class = "font-color-blue" })`
<a class="font-color-blue" href="/Trip/Travel/Index/Index?par1=test&par2=test2">Click Here</a>
 context.MapRoute(
            "EPloeg_default",
            "EPloeg/{controller}/{tabmenu}/{action}/{id}/{actionMethod}",
            new { action = "Index", id = UrlParameter.Optional, actionMethod = UrlParameter.Optional }
        );   
哪个将生成此html:

@Html.CustomActionLink("LinkText", "Area","Controller","TabMenu","Action",routeValues, htmlAttributes)
 @Html.CustomActionLink("Click here","Travel","Trip","Index","Index", new { par1 = "test", par2 = test2, new { @class = "font-color-blue" })`
<a class="font-color-blue" href="/Trip/Travel/Index/Index?par1=test&par2=test2">Click Here</a>
 context.MapRoute(
            "EPloeg_default",
            "EPloeg/{controller}/{tabmenu}/{action}/{id}/{actionMethod}",
            new { action = "Index", id = UrlParameter.Optional, actionMethod = UrlParameter.Optional }
        );   

有什么想法吗?

您可以实现自定义操作链接扩展,您必须在
LinkExtensions
类中编写自己的方法:

namespace TestCustomHelper.Html
{

public static class LinkExtensions
{
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool showActionLinkAsDisabled)
  {
     if (htmlHelper.ActionAuthorized(actionName, controllerName))
     {
       return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
     }
     return MvcHtmlString.Empty;
}

}
注: 我已经添加了一个extara bool参数,如果您看到该方法的最后一个参数,您可以根据需要添加更多

我只是根据需要为它编写了一个重载,您可以将所有重载编写为
Html.ActionLink()
has

看我的

也看到

使现代化
您可能想查看我的

您可以实现自定义操作链接扩展,您必须在
LinkExtensions
类中编写自己的方法:

namespace TestCustomHelper.Html
{

public static class LinkExtensions
{
public static MvcHtmlString ActionLinkAuthorized(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool showActionLinkAsDisabled)
  {
     if (htmlHelper.ActionAuthorized(actionName, controllerName))
     {
       return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
     }
     return MvcHtmlString.Empty;
}

}
注: 我已经添加了一个extara bool参数,如果您看到该方法的最后一个参数,您可以根据需要添加更多

我只是根据需要为它编写了一个重载,您可以将所有重载编写为
Html.ActionLink()
has

看我的

也看到

使现代化
您可能想看看我的

下面的代码如何

@Html.ActionLink("Click here","Trip","Index", new { area= "Travel", tabmenu= "Index"}, new { @class = "font-color-blue" })

编辑

您可以使用这样的扩展方法

public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string linkText, string area, string controller, string tabMenu, string action, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
    routeValues.Add("area", area);
    routeValues.Add("tabMenu", tabMenu);
    return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
}
public static MvcHtmlString CustomActionLink(此HtmlHelper、字符串链接文本、字符串区域、字符串控制器、字符串选项卡菜单、字符串操作、RouteValueDictionary routeValues、IDictionary htmlAttributes)
{
RouteValue.添加(“区域”,区域);
添加(“tabMenu”,tabMenu);
返回htmlHelper.ActionLink(linkText、actionName、controllerName、RouteValue、htmlAttributes);
}

下面的代码怎么样

@Html.ActionLink("Click here","Trip","Index", new { area= "Travel", tabmenu= "Index"}, new { @class = "font-color-blue" })

编辑

您可以使用这样的扩展方法

public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string linkText, string area, string controller, string tabMenu, string action, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
{
    routeValues.Add("area", area);
    routeValues.Add("tabMenu", tabMenu);
    return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
}
public static MvcHtmlString CustomActionLink(此HtmlHelper、字符串链接文本、字符串区域、字符串控制器、字符串选项卡菜单、字符串操作、RouteValueDictionary routeValues、IDictionary htmlAttributes)
{
RouteValue.添加(“区域”,区域);
添加(“tabMenu”,tabMenu);
返回htmlHelper.ActionLink(linkText、actionName、controllerName、RouteValue、htmlAttributes);
}

这会生成什么html?Sry忘记了,在问题中添加了它!这会生成什么html?Sry忘记了,在问题中添加了它!我希望它更干净,这就是为什么当我有很多参数时,我不希望路由值中有area和tabmenu。然后只需要创建一个扩展方法,并在内部执行此操作。检查我的编辑以获得示例扩展方法显然参数没有正确传递。这就是我调用actionlink的方式:@Html.CustomActionLink(EnumTranslator.Translate(item.TypeEvaluation),“EPloeg”,“Evaluation”,Model.TabMenu,“Detail”,new RouteValueDictionary(new{id=item.id,actionMethod=Model.actionMethod}))如果传递所有
string
值,它传递是否正确?然后检查哪些值没有通过我希望它更干净,这就是为什么当我有很多参数时,我不希望路由值中有area和tabmenu。然后只需创建一个扩展方法并在内部执行此操作,检查我对示例扩展方法的编辑显然参数没有正确通过。这就是我调用actionlink的方式:@Html.CustomActionLink(EnumTranslator.Translate(item.TypeEvaluation),“EPloeg”,“Evaluation”,Model.TabMenu,“Detail”,new RouteValueDictionary(new{id=item.id,actionMethod=Model.actionMethod}))如果传递所有
string
值,它传递是否正确?并检查哪些值未通过