首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ServiceStack Funq MVC html helper扩展

ServiceStack Funq MVC html helper扩展
EN

Stack Overflow用户
提问于 2013-03-22 21:10:39
回答 1查看 281关注 0票数 2

我正在尝试创建一个MVC html helper扩展,它必须声明为静态类,如下所示:

代码语言:javascript
复制
public static class PhotoExtension
{
    public static IPhotoService PhotoService { get; set; }
    public static IGalleryService GalleryService { get; set; }

    public static MvcHtmlString Photo(this HtmlHelper helper, int photoId, string typeName)
    {
         //[LOGIC GOES HERE]
         return new MvcHtmlString(..some resulting Html...);
    }
}

现在,我想在Photo()方法中使用IPhotoService和IGalleryService。到目前为止,我发现如何在AppHost.Configure()中注入这些服务的唯一方法:

代码语言:javascript
复制
PhotoExtension.PhotoService = container.Resolve<IPhotoService>();
PhotoExtension.GalleryService = container.Resolve<IGalleryService>();

这是有效的,尽管我好奇是否有更好的方法来实现这一点。

IPhotoServiceIGalleryService都是在AppHost.Configure()中以标准方式注册的。

谢谢,安东宁

EN

回答 1

Stack Overflow用户

发布于 2013-05-31 06:07:43

更容易阅读/遵循,将它们连接到静态构造函数中?

代码语言:javascript
复制
using ServiceStack.WebHost.Endpoints;

public static class PhotoExtension
{
    public static IPhotoService PhotoService { get; set; }
    public static IGalleryService GalleryService { get; set; }

    static PhotoExtension()
    {
        PhotoService = EndpointHost.AppHost.TryResolve<IPhotoService>();
        GalleryService  = EndpointHost.AppHost.TryResolve<IGalleryService>();
    }

    public static MvcHtmlString Photo(this HtmlHelper helper, int photoId, string typeName)
    {
     //[LOGIC GOES HERE]
     return new MvcHtmlString(..some resulting Html...);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15571190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档