如何在c#代码中使用MVC 3 @ Html.ActionLink

ron*_*san 15 c# model-view-controller asp.net-mvc html.actionlink asp.net-mvc-3

我想在ac#函数中调用@ Html.ActionLink方法来返回一个带有链接的字符串.

像这样的东西:

string a = "Email is locked, click " + @Html.ActionLink("here to unlock.", "unlock") ;
Run Code Online (Sandbox Code Playgroud)

cou*_*ben 8

假设您想在控制器中完成此操作,可以通过几个环节来跳过.你必须实例化a ViewDataDictionary和a TempDataDictionary.然后你需要采取ControllerContext并创建一个IView.最后,您已准备好HtmlHelper使用所有这些元素(以及您的RouteCollection)创建.

完成所有这些操作后,您可以使用LinkExtensions.ActionLink创建自定义链接.在您的视图中,您需要使用@Html.Raw()显示链接,以防止它们被HTML编码.这是必要的代码:

var vdd = new ViewDataDictionary();
var tdd = new TempDataDictionary();
var controllerContext = this.ControllerContext;
var view = new RazorView(controllerContext, "/", "/", false, null);
var html = new HtmlHelper(new ViewContext(controllerContext, view, vdd, tdd, new StringWriter()),
     new ViewDataContainer(vdd), RouteTable.Routes);
var a = "Email is locked, click " + LinkExtensions.ActionLink(html, "here to unlock.", "unlock", "controller").ToString();
Run Code Online (Sandbox Code Playgroud)

在展示了所有这些之后,我会提醒您,在您的视图中执行此操作会更好.将错误和其他信息添加到ViewModel,然后对视图进行编码以创建链接.如果需要跨多个视图,请创建HtmlHelper以创建链接.

UPDATE

为了解决one.beat.consumer,我的初步答案是可能的例子.如果开发人员需要重用这种技术,那么复杂性可以隐藏在静态助手中,如下所示:

public static class ControllerHtml
{
    // this class from internal TemplateHelpers class in System.Web.Mvc namespace
    private class ViewDataContainer : IViewDataContainer
    {
        public ViewDataContainer(ViewDataDictionary viewData)
        {
            ViewData = viewData;
        }

        public ViewDataDictionary ViewData { get; set; }
    }

    private static HtmlHelper htmlHelper;

    public static HtmlHelper Html(Controller controller)
    {
        if (htmlHelper == null)
        {
            var vdd = new ViewDataDictionary();
            var tdd = new TempDataDictionary();
            var controllerContext = controller.ControllerContext;
            var view = new RazorView(controllerContext, "/", "/", false, null);
            htmlHelper = new HtmlHelper(new ViewContext(controllerContext, view, vdd, tdd, new StringWriter()),
                 new ViewDataContainer(vdd), RouteTable.Routes);
        }
        return htmlHelper;
    }

    public static HtmlHelper Html(Controller controller, object model)
    {
        if (htmlHelper == null || htmlHelper.ViewData.Model == null || !htmlHelper.ViewData.Model.Equals(model))
        {
            var vdd = new ViewDataDictionary();
            vdd.Model = model;
            var tdd = new TempDataDictionary();
            var controllerContext = controller.ControllerContext;
            var view = new RazorView(controllerContext, "/", "/", false, null);
            htmlHelper = new HtmlHelper(new ViewContext(controllerContext, view, vdd, tdd, new StringWriter()),
                 new ViewDataContainer(vdd), RouteTable.Routes);
        }
        return htmlHelper;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,在控制器中,它像这样使用:

var a = "Email is locked, click " + 
    ControllerHtml.Html(this).ActionLink("here to unlock.", "unlock", "controller").ToString();
Run Code Online (Sandbox Code Playgroud)

或者像这样:

var model = new MyModel();
var text = ControllerHtml.Html(this, model).EditorForModel();
Run Code Online (Sandbox Code Playgroud)

虽然它更容易使用Url.Action,但现在扩展为一个强大的工具,使用所有HtmlHelper(具有完整的Intellisense)在控制器内生成任何标记.

使用的可能性包括使用模型生成标记和电子邮件的编辑模板,pdf生成,在线文档传递等.


hun*_*ter 6

您可以创建一个HtmlHelper扩展方法:

public static string GetUnlockText(this HtmlHelper helper)
{
    string a = "Email is locked, click " + helper.ActionLink("here to unlock.", "unlock");
    return a;
}
Run Code Online (Sandbox Code Playgroud)

或者如果您要在aspx页面范围之外生成此链接,则需要创建对HtmlHelper的引用,然后生成.我在一个UrlUtility静态类中这样做(我知道,人们讨厌静态类和单词Utility,但试着集中注意力).必要时超载:

public static string ActionLink(string linkText, string actionName, string controllerName)
{
    var httpContext = new HttpContextWrapper(System.Web.HttpContext.Current);
    var requestContext = new RequestContext(httpContext, new RouteData());
    var urlHelper = new UrlHelper(requestContext);

    return urlHelper.ActionLink(linkText, actionName, controllerName, null);
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以从心愿的任何地方写下以下内容:

string a = "Email is locked, click " + UrlUtility.ActionLink("here to unlock.", "unlock", "controller");
Run Code Online (Sandbox Code Playgroud)


one*_*mer 5

这些其他答案有几件坏事......

Shark的回答要求你将LinkExtensions命名空间带入C#,这没有错,但对我来说是不可取的.

Hunter创建帮助器的想法更好,但仍然为单个URL编写辅助函数很麻烦.您可以编写帮助程序来帮助您构建接受参数的字符串,或者您可以简单地以旧方式执行:

var link = "Email is... click, <a href=\"" + Url.Action("action") + "\"> to unlock.</a>";
Run Code Online (Sandbox Code Playgroud)

@counsellorben,

我认为没有理由这么复杂; 用户只想将Action的路由呈现为包含锚标记的硬字符串.此外,硬编写的连接字符串中的ActionLink()只能购买一个,并强制开发人员使用专用于视图的LinkExtensions.

如果用户很难使用ActionLink()或者不需要(由于某种原因)在构造函数中计算此字符串,那么在视图中这样做会好得多.

我仍然袖手旁观并推荐tvanfosson和我提供的答案.