Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access model/request data with servicestack razor from a _Layout page

I have a shared _Layout.cshtml page where I need to get access to the service that is invoking that page, or access to either the request dto or Model so that I can render different options depending on the type of the calling service, the model type or the request type. I cannot seem to get access any of the above while in the _Layout page. I'm pretty new to SS-Razor and am probably just not seeing the bigger picture, but here is what I have done so far to try and access the model.

In the _Layout.cshtml, I have defined the following, so I can get access to the ViewBag and also the Session information.

@using ServiceStack.Razor
@inherits ViewPage<ResponseBase>

I have tried just a plain view page also

@inherits ViewPage

This give me access to the basic page infrastructure in the _Layout. But the Model in the layout is ALWAYS null, even though temporially I changed all the responses to inherit from a ResponseBase class and as can be seen in the code above I inherit that page where all the Models would have a ResponseBase at least.

I then set-up a global filter where I inject the request DTO into the request items as that would be a start and would prove that I could access some information while in the Layout. So I added the following to the app host,

this.RequestFilters.Add((httpReq, httpResp, requestDto) =>
{
   if(httpReq != null && httpReq.Items != null)
                    httpReq.Items["RequestDto"] = requestDto;
}); 

I can see a breakpoint been hit on the filter and can see the reqestDto been addedd to the collection so I know this piece at least is working. When I tried to access the requestDto in the _Layout using the code below the Request.Items never contain the RequestDto.

@{
    RequestBase myRequest = null;
    if(base.Request != null)
        if(base.Request.Items.ContainsKey("RequestDto"))
            myRequest = (RequestBase)base.Request.Items["RequestDto"];
}

All this is somewhat trial and error because for the life of me I can't get a breakpoint to set while in a Razor View Page so I can see what is actially going on in there. I've been through the RazorRockStars sample a gazillion times at this stage and can't see any example of what I am trying to do in there.

So does anyone have any pointers, examples or advice on how to tackle this issue. Feel free to point out an entirely different approach for rendering different options in the _Layout as at this stage I don't have much hair left and am willing to try just about anything.

like image 468
Bigtoe Avatar asked Nov 13 '22 16:11

Bigtoe


1 Answers

Try out v3.9.33 of ServiceStack. mythz fixed some things in the razor support that might be related to this issue.

like image 70
jeffgabhart Avatar answered Dec 06 '22 07:12

jeffgabhart