扩展CommonViewModel以包含其他属性

时间:2015-07-07 11:00:08

标签: c# custom-view thinktecture-ident-server identityserver3

我有一个非常简单的问题:根据我部署Identity Server 3的环境,我希望让登录页面显示不同的图像。图像源位置在web.config中定义,并根据构建配置进行转换。

我重写部分_login.html以允许显示图像,但我无法找到如何(轻松)将定义此配置变量的变量传递给视图。

我相信这可以通过使用此处显示的CustomViewService来实现:http://identityserver.github.io/Documentation/docs/advanced/customizingViews.html但我想知道是否有更简单的方法将变量传递到视图中而无需创建{ {1}}。

是否可以扩展CustomViewService并让Identity Server 3在运行时解决此问题? CommonViewModel非常简单:

ExtendedCommonViewModel

然后在public class ExtendedCommonViewModel : CommonViewModel { public string ImageLocation { get { return ConfigurationManager.AppSettings["ImageLocation"].ToString(); } } } 视图中将其用作_login.html

我意识到这仍然需要解析为默认视图服务中的<img src="{ImageLocation}" />函数,因此这就是这个想法失败的地方:https://github.com/IdentityServer/IdentityServer3/blob/master/source/Core/Services/DefaultViewService/DefaultViewService.cs

重申;是否有一种简单的方法可以将变量传递到Identity Server 3中的重写部分视图,而无需创建Replace

修改:

附加 - 我可以通过在CustomViewService上将变量作为Scripts参数传递来看到解决方案的黑客攻击,但这看起来太糟糕了。

可以扩展DefaultViewServiceOptions并添加一个自定义词典,该词典可以包含随后可以在视图中使用的自定义模型参数。这似乎是最好的解决方案,但意味着更改DefaultViewServiceOptions

1 个答案:

答案 0 :(得分:0)

您最好创建自己的IViewService实施。

可以继承DefaultViewService并仅覆盖您需要更改的内容。

在您的情况下,我认为您可以使用您需要的任何数据创建从CustomLoginViewModel继承的LoginViewModel

public class CustomViewService : DefaultViewService 
{
   ...    
    public override Task<Stream> Login(LoginViewModel model, SignInMessage message)
    {
        var customVm = new CustomViewModel { MyProp = "123" };
        return Render(customVm, LoginView);
    }
}