简体   繁体   English

如何从asp.net mvc 2项目中的非控制器静态类生成URL?

[英]How can I generate url from non-controller static class in asp.net mvc 2 project?

I have created Html helper class in asp.net mvc2 project: 我在asp.net mvc2项目中创建了HTML帮助器类:

public static class CaptionExtensions
{
    public static string Captions(this HtmlHelper helper, Captions captions)
    {
        var sb = new StringBuilder();
        sb.AppendLine("<ul>");

        foreach (var caption in captions)
        {
            //  var url = Url.Action("CaptionCategory", new {id = caption.Code} )

            sb.AppendLine("<li>");
            sb.AppendLine(  "<a href="+ url + ">");
            sb.AppendLine(      caption);
            sb.AppendLine(  "</a>");
            sb.AppendLine("</li>");
        }

        sb.AppendLine("</ul>");


        return sb.ToString();
    }
}

I need to generate url similar with the way in commented line. 我需要生成与注释行中的方式类似的网址。 Commented code is how I do it in the controller class, but this is helper class (static context). 注释的代码是我在控制器类中的工作方式,但这是辅助类(静态上下文)。 Any help??? 有帮助吗???

Simply create an UrlHelper out of the RequestContext property of the HtmlHelper and use it to generate urls: 只需从HtmlHelper的RequestContext属性中创建一个UrlHelper并使用它来生成url:

var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.Action("CaptionCategory", new { id = caption.Code });

or in your particular case use the html helper to generate the anchor instead of hardcoding it as you did: 或者在您的特定情况下,使用html helper生成锚,而不是像您那样对其进行硬编码:

sb.AppendLine("<li>");
sb.AppendLine(
    helper.ActionLink(
        caption, 
        "CaptionCategory", 
        new { id = caption.Code }
    ).ToHtmlString()
);
sb.AppendLine("</li>");

For this to work you should obviously add using System.Web.Mvc.Html; 为此,您显然应该using System.Web.Mvc.Html;添加using System.Web.Mvc.Html; to the top of your file in order to bring the ActionLink extension method into scope. 到文件顶部,以便将ActionLink扩展方法纳入范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将配置传递给ASP.NET MVC6中的非控制器类 - How to pass Configuration to non-controller classes in ASP.NET MVC6 如何在ASP.NET页的每个控制器中使用非静态类的相同实例? - How can I use the same instance of a non-static class in every controller in an ASP.NET page? 是否可以重定向到MVC中的非控制器类进行查看 - Is it possible to redirect to view from a non-controller class in MVC 如何从ASP.NET MVC网站中生成可下载的c#类? - How can I generate a downloadable c# class from within an ASP.NET MVC website? 我如何在asp.net MVC中从URL更改隐藏控制器名称 - how can i change-hide controller name from url in asp.net MVC 如何在ASP.NET MVC 3项目的服务层生成URL - How to generate URL in service layer of ASP.NET MVC 3 project 如何在 ASP.NET MVC 中的控制器外部生成 URL? - How do I generate a URL outside of a controller in ASP.NET MVC? 我如何引用MVC项目中的类,该类使用asp.net核心从数据库中检索数据 - How can I reference a class in MVC project that retrieves data from database with asp.net core 如何将数据从asp.net MVC控制器发布到非MVC asp.net页面? - How do I POST data from an asp.net MVC controller to a non-MVC asp.net page? 如何在ASP.NET MVC 5.1项目中通过控制器的动作使用标准JSON格式器? - How can I use the standard JSON formatter from an action of my controller in an ASP.NET MVC 5.1 project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM