Matze
Matze

Reputation: 5518

How to return a view result without layout from a ServiceStack service?

I have a ServiceStack.Service implementation that defines a method that must return HTML markup, but a fragment only. I tried to just set the View property of the an HttpResult object without specifying a layout in the Razor view nor do I set the Template property of the result - and I expected to get the view markup only, which is not the case.

The view is derived from ViewPage. The service function looks like that...

public class SampleService : Service
{
    public IHttpResult Get(GetSampleRequest request)
    {
        return new HttpResult
        {
            View = "SampleView"
        };
    }
}

The problem is that ServiceStack tries to find a default layout (which exists) and uses that. How can I prevent that? In ASP.NET MVC I would usually just return the result from the Partial extension method; does something similiar exist in ServiceStack?

Upvotes: 3

Views: 1685

Answers (3)

Mark Schultheiss
Mark Schultheiss

Reputation: 34227

You could also set a layout that contains nothing or just a comment to indicate it was deliberate "_LayoutEmpty.cshtml". Kind of a design choice vs an empty string name.

Upvotes: 2

Geoff James
Geoff James

Reputation: 3180

You will need to define a Layout in your Razor View as empty.

However, setting it to null just defaults to using the _Layout.cshtml page (or whatever's defined as your default).

In your Razor View, set the Layout to "", like so:

@{
    Layout = "";
 }

You could also set Layout = string.Empty. Both have the same desired effect.

Hope this helps! :)

Upvotes: 5

Georg Patscheider
Georg Patscheider

Reputation: 9463

In the Razor View, add

@{ Layout = null; }

Upvotes: 1

Related Questions