小编Cem*_*sha的帖子

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

我在 asp.net mvc2 项目中创建了 Html helper 类:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要生成与注释行中的方式类似的 url。注释代码是我在控制器类中的做法,但这是辅助类(静态上下文)。任何帮助???

c# html-helper routevalues asp.net-mvc-2 url-action

2
推荐指数
1
解决办法
2767
查看次数

标签 统计

asp.net-mvc-2 ×1

c# ×1

html-helper ×1

routevalues ×1

url-action ×1