How can I add rel=\"nofollow to my ActionLink?

2019-08-31 01:45发布

问题:

I have the following:

@Html.ActionLink("Register", "Register", "Users")

I would like to make the link include the following:

title="Register your ID" 
rel="nofollow"

Is there a way that I can do this with MVC3?

回答1:

Simply use the proper overload of the ActionLink method:

@Html.ActionLink(
    "Register",                           // linkText
    "Register",                           // actionName
    "Users",                              // controllerName
    null,                                 // routeValues
    new {                                 // htmlAttributes
        title = "Register your ID", 
        rel = "nofollow" 
    }
)

Should generate (assuming default routes setup):

<a href="/Users/Register" rel="nofollow" title="Register your ID">Register</a>


回答2:

@Html.ActionLink("Register", "Register", "Users", null,
    new {title = "Register your ID", rel="nofollow")

The framework will parse that object and place the items as attributes in the rendered anchor.