servicestack,dto,Razor,Inversion Of Control,servicestack,Dto" /> servicestack,dto,Razor,Inversion Of Control,servicestack,Dto" />

Razor ServiceStack解析服务

Razor ServiceStack解析服务,razor,inversion-of-control,servicestack,dto,Razor,Inversion Of Control,servicestack,Dto,因此,我的问题是调用下面url中描述的apphost.ResolveService: 我在我的_Layout.cshtml中 显然,下面的代码工作得很好,但正如上面url中的答案所建议的,这有点愚蠢 SMSGateway.Services.EntityCollectionResponse= 新ServiceStack.ServiceClient.Web.JsonServiceClient( "http://localhost:1337/") .得到( “/实体”); 这给了我一个实体列表:)

因此,我的问题是调用下面url中描述的apphost.ResolveService:

我在我的_Layout.cshtml中

显然,下面的代码工作得很好,但正如上面url中的答案所建议的,这有点愚蠢

SMSGateway.Services.EntityCollectionResponse=
新ServiceStack.ServiceClient.Web.JsonServiceClient(
"http://localhost:1337/")
.得到(
“/实体”);
这给了我一个实体列表:)但不是最优的。。。因此,我尝试用正确的方式来做这件事

var response = ConsoleAppHost.Instance
    .ResolveService<SMSGateway.Services.EntityService>(
        HttpContext.Current).Get(
            new SMSGateway.Services.EntitiesRequest());

// SMSGateway.Services.EntityCollectionResponse response =
//     base.Get<SMSGateway.Services.EntityService>().Get(
//         new SMSGateway.Services.EntitiesRequest());

foreach (var entity in response.Result)
{
    <li>
        <a href="@entity.MetaLink.Href">
            @Html.TitleCase(entity.Name) entities
        </a>
    </li>
}
var response=ConsoleAppHost.Instance
.解决方案服务(
HttpContext.Current)。获取(
新的SMSGateway.Services.EntitiesRequest());
//SMSGateway.Services.EntityCollectionResponse响应=
//base.Get().Get(
//新的SMSGateway.Services.EntitiesRequest());
foreach(响应中的var实体.Result)
{
  • }
    好的,我得到的错误如下:

    错误CS0122:ConsoleAppHost由于其保护而不可访问 水平

    这是预期的吗?我在想这是否是一个不允许我在_Layout.cshtml文件中调用它的情况

    进一步阅读使我想到了这篇文章


    我发现这很有趣:p但没有雪茄:)

    我建议您不要在Razor模板中调用服务。Razor模板应仅用于呈现模型中的某些标记

    实际数据访问应在呈现此模板的ServiceStack服务中执行。因此,在您的情况下,您可以从操作中调用其他服务:

    public object Get(SomeRequestDto message)
    {
        var response = this
            .ResolveService<SMSGateway.Services.EntityService>()
            .Get(new SMSGateway.Services.EntitiesRequest()
        );
    
        return response.Rersult;
    }
    
    然后,Razor视图当然会被强类型化为相应的模型:

    @model IEnumerable<WhateverTheTypeOfTheResultYouWannaBeLoopingThrough>
    foreach (var entity in Model)
    {
        <li>
            <a href="@entity.MetaLink.Href">
                @Html.TitleCase(entity.Name) entities
            </a>
        </li>
    }
    
    @model IEnumerable
    foreach(模型中的var实体)
    {
    
  • }
    我建议您不要在Razor模板中调用服务。Razor模板应仅用于呈现模型中的某些标记

    实际数据访问应在呈现此模板的ServiceStack服务中执行。因此,在您的情况下,您可以从操作中调用其他服务:

    public object Get(SomeRequestDto message)
    {
        var response = this
            .ResolveService<SMSGateway.Services.EntityService>()
            .Get(new SMSGateway.Services.EntitiesRequest()
        );
    
        return response.Rersult;
    }
    
    然后,Razor视图当然会被强类型化为相应的模型:

    @model IEnumerable<WhateverTheTypeOfTheResultYouWannaBeLoopingThrough>
    foreach (var entity in Model)
    {
        <li>
            <a href="@entity.MetaLink.Href">
                @Html.TitleCase(entity.Name) entities
            </a>
        </li>
    }
    
    @model IEnumerable
    foreach(模型中的var实体)
    {
    
  • }
    我正在寻找允许我使用另一个“RenderBody”的东西。。。。因为在_layout.cshtml中,我似乎无法从我的服务中获取信息/模型,除非我调用“RenderBody”,而且仅此一项就允许每个视图只有一个模型。。。那么,如何为我的布局提供不同的模型呢?这就是我选择在Razor中使用JSONClient的原因……我正在寻找允许我使用另一个“RenderBody”的东西。。。。因为在_layout.cshtml中,我似乎无法从我的服务中获取信息/模型,除非我调用“RenderBody”,而且仅此一项就允许每个视图只有一个模型。。。那么,如何为我的布局提供不同的模型呢?这就是我选择在Razor中使用JSONClient的原因。。。。