Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the link below on a razor page:

@Html.ActionLink("Create New Profile", "Create", "Profile", new { @class="toplink" })

It appears in thes source of view page as shown below:

<a href="/admin/profile/create?length=7" class="toplink">Create New Profile</a>

When I click on the link the URL is like this:

http://localhost:54876/admin/profile/create?length=7

I don't want ?length=7. Why is this auto generated?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
717 views
Welcome To Ask or Share your Answers For Others

1 Answer

The ActionLink override you are using matches to the (string linkText, string actionName, Object routeValues, Object htmlAttributes) override. So your "Profile" value is being passed to the routeValues parameter. The behavior of this function with respect to this parameter is to take all public properties on it and add it to the list of route values used to generate the link. Since a String only has one public property (Length) you end up with "length=7".

The correct overload you want to use is the (string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes) and you call it loke so:

@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink"})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...