本文翻译自:HTML.ActionLink vs Url.Action in ASP.NET Razor
Is there any difference between HTML.ActionLink
vs Url.Action
or they are just two ways of doing the same thing? HTML.ActionLink
与Url.Action
之间是否有区别,或者它们只是做同一件事的两种方式?
When should I prefer one over the other? 我什么时候比另一个更喜欢?
#1楼
参考:https://stackoom.com/question/wlsJ/ASP-NET-Razor中的HTML-ActionLink与Url-Action
#2楼
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("Index", "Company", FormMethod.Get))
{
<p>
Find by Name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
<input type="button" value="Clear" onclick="location.href='@Url.Action("Index","Company")'"/>
</p>
}
In the above example you can see that If I specifically need a button to do some action, I have to do it with @Url.Action whereas if I just want a link I will use @Html.ActionLink. 在上面的示例中,您可以看到,如果我特别需要一个按钮来执行某些操作,则必须使用@ Url.Action进行操作,而如果我只想要一个链接,则将使用@ Html.ActionLink。 The point is when you have to use some element(HTML) with action url is used. 关键是当您必须使用某些元素(HTML)和操作网址时。
#3楼
Html.ActionLink
generates an <a href=".."></a>
tag automatically. Html.ActionLink
自动生成一个<a href=".."></a>
标记。
Url.Action
generates only an url. Url.Action
仅生成一个URL。
For example: 例如:
@Html.ActionLink("link text", "actionName", "controllerName", new { id = "<id>" }, null)
generates: 产生:
<a href="/controllerName/actionName/<id>">link text</a>
and 和
@Url.Action("actionName", "controllerName", new { id = "<id>" })
generates: 产生:
/controllerName/actionName/<id>
Best plus point which I like is using Url.Action(...)
我喜欢的最佳加分点是使用Url.Action(...)
You are creating anchor tag by your own where you can set your own linked text easily even with some other html tag. 您正在按自己的方式创建定位标记,即使使用其他html标记,也可以轻松设置自己的链接文本。
<a href="@Url.Action("actionName", "controllerName", new { id = "<id>" })">
<img src="<ImageUrl>" style"width:<somewidth>;height:<someheight> />
@Html.DisplayFor(model => model.<SomeModelField>)
</a>
#4楼
我使用下面的代码创建了一个Button,它对我有用。
<input type="button" value="PDF" onclick="location.href='@Url.Action("Export","tblOrder")'"/>
#5楼
You can easily present Html.ActionLink as a button by using the appropriate CSS style. 您可以使用适当的CSS样式轻松地将Html.ActionLink呈现为按钮。 For example: 例如:
@Html.ActionLink("Save", "ActionMethod", "Controller", new { @class = "btn btn-primary" })
#6楼
@HTML.ActionLink
generates a HTML anchor tag
. @HTML.ActionLink
生成HTML anchor tag
。 While @Url.Action
generates a URL
for you. @Url.Action
为您生成一个URL
。 You can easily understand it by; 您可以很容易地理解它;
// 1. <a href="/ControllerName/ActionMethod">Item Definition</a>
@HTML.ActionLink("Item Definition", "ActionMethod", "ControllerName")
// 2. /ControllerName/ActionMethod
@Url.Action("ActionMethod", "ControllerName")
// 3. <a href="/ControllerName/ActionMethod">Item Definition</a>
<a href="@Url.Action("ActionMethod", "ControllerName")"> Item Definition</a>
Both of these approaches are different and it totally depends upon your need. 这两种方法都不相同,这完全取决于您的需求。