asp.net-mvc 从控制器内部使用 Html.ActionLink 和 Url.Action(...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2617064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Html.ActionLink and Url.Action(...) from inside Controller
提问by Petrus Theron
I want to write an HtmlHelper to render an ActionLink with pre-set values, eg.
我想编写一个 HtmlHelper 来呈现具有预设值的 ActionLink,例如。
<%=Html.PageLink("Page 1", "page-slug");%>
where PageLink
is a function that calls ActionLink
with a known Action and Controller, eg. "Index" and "Page".
哪里PageLink
是一个调用ActionLink
已知动作和控制器的函数,例如。“索引”和“页面”。
Since HtmlHelper
and UrlHelper
do not exist inside a Controller
or class, how do I get the relative URL to an action from inside a class?
由于HtmlHelper
和UrlHelper
不存在于Controller
或类中,如何从类中获取操作的相对 URL?
Update:Given the additional three years of accrued experience I have now, here's my advice: just use Html.ActionLink("My Link", new { controller = "Page", slug = "page-slug" })
or better yet,
更新:鉴于我现在拥有的额外三年积累的经验,这是我的建议:只需使用Html.ActionLink("My Link", new { controller = "Page", slug = "page-slug" })
或更好,
<a href="@Url.Action("ViewPage",
new {
controller = "Page",
slug = "my-page-slug" })">My Link</a>
Your extension method may be cute and short, but it adds another untested point-of-failure and a new learning requirement for hires without adding any real value whatsoever. Think of it as designing a complex system. Why add another moving part, unless it adds reliability (no), readability (little, once you read moredocs), speed (none) or concurrency (none).
你的扩展方法可能既可爱又简短,但它增加了另一个未经测试的失败点和新的员工学习要求,而没有增加任何真正的价值。把它想象成设计一个复杂的系统。为什么要添加另一个活动部分,除非它增加了可靠性(无)、可读性(很少,一旦你阅读更多文档)、速度(无)或并发性(无)。
回答by jweyrich
Not sure I actually understood your question clearly, but, let me try.
不确定我是否真的清楚地理解了您的问题,但是,让我尝试一下。
To create a HtmlHelper extension like you described, try something like:
要像您描述的那样创建 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);
}
}
}
As for your question on getting a URL from a class, depends on what kind of class you'll implement it. For example, if you want to get the current controller and action from a HtmlHelper extension, you can use:
至于您从类中获取 URL 的问题,取决于您将实现它的类。例如,如果您想从 HtmlHelper 扩展中获取当前控制器和操作,您可以使用:
string currentControllerName = (string)helper.ViewContext
.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext
.RouteData.Values["action"];
If you want to get it from a controller, you can use properties/methods from the base class (Controller) to build the URL. For example:
如果你想从控制器获取它,你可以使用基类 (Controller) 中的属性/方法来构建 URL。例如:
var url = new UrlHelper(this.ControllerContext.RequestContext);
url.Action(an_action_name, route_values);