如何使用 ServiceStack 模板支持基于请求类型的动态结果?
How to use ServiceStack Templates to support dynamic results based on request type?
和ServiceStack's Razor Story we have a variety of ways of selecting which Razor View we want to use to render a page. Even better, and critical in my case, is we can pass in a Content-Type header (or query string parameter, or even page "suffix") as well to return the raw model in a variety of formats.
有什么方法可以使用 ServiceStack Templates (now known as SharpScript) to do the same thing? I follow the example here 但我只是得到标准 HTML 格式的响应。它不使用我的模板,无论如何命名。
按照 v5.5 Release Notes 中的示例:
[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
public class HelloService : Service
{
public object Any(Hello request) => new HelloResponse { Result = $"Hello, {request.Name}!" };
}
转到 /hello/World?format=html
为我提供了标准 HTML 报告,而不是我的模板。我按照另一个例子强制它使用模板....
public object Any(Hello request) =>
new PageResult(Request.GetPage("examples/hello")) {
Model = request.Name
};
...它总是 returns 我的模板,即使我指定 /hello/World?format=json
.
有什么方法可以让 ServiceStack + ScriptSharp 页面有类似 Razor 的视图选择,而且还支持不同的响应格式?
如果没有您想要实现的特定场景的详细信息,很难回答这样一个模糊的问题。
您可以通过多种方式returnSharp Pages:
- 直接作为内容页面请求时,例如
/dir/page
-> /dir/page.html
- 使用 Page Based Routing,例如
/dir/1
-> /dir/_id.html
- 作为 View Page 响应以 Request DTO 或 Response DTO 命名的服务,例如
/contacts/1
-> /Views/GetContact.html
或 /Views/GetContactResponse.html
Select 通过在自定义 HttpResult
:
中 returning 你的 Response DTO 来在你的服务中呈现哪个视图
public object Any(MyRequest request)
{
...
return new HttpResult(response)
{
View = "CustomPage", // -> /Views/CustomPage.html
//Template = "_custom-layout",
};
}
添加 [ClientCanSwapTemplates]
Request Filter 属性,让 View 和 Template 修改 QueryString,例如:?View=CustomPage&Template=_custom-layout
[ClientCanSwapTemplates]
public object Any(MyRequest request) => ...
通过 return 自定义 PageResult
:
选择要在 Model View Controller Service 中呈现的页面
public class CustomerServices : Service
{
public object Any(ViewCustomer request) =>
new PageResult(Request.GetPage("examples/customer")) {
Model = TemplateQueryData.GetCustomer(request.Id)
};
}
Note: That the SharpPagesFeature
resolves pages using your cascading AppHost.VirtualFileSources
. In .NET Core it's configured to use its WebRoot, e.g /wwwroot
.
对于 Sharp Pages return 其在多种内容类型中的响应:
as well to return the raw model in a variety of formats.
您需要使用 Sharp APIs which return
a value, e.g. /hello/_name/index.html:
{{ { result: `Hello, ${name}!` } | return }}
为了简洁地回答我自己的问题,@mythz 的第一个选项就是我所需要的。在我的 AppHost
中调用 Plugins.Add(new SharpPagesFeature())
后,我需要从我的服务方法 return HttpResult
:
public object Any(MyRequest request)
{
...
return new HttpResult(response)
{
View = "CustomPage", // -> /Views/CustomPage.html
//Template = "_custom-layout",
};
}
和ServiceStack's Razor Story we have a variety of ways of selecting which Razor View we want to use to render a page. Even better, and critical in my case, is we can pass in a Content-Type header (or query string parameter, or even page "suffix") as well to return the raw model in a variety of formats.
有什么方法可以使用 ServiceStack Templates (now known as SharpScript) to do the same thing? I follow the example here 但我只是得到标准 HTML 格式的响应。它不使用我的模板,无论如何命名。
按照 v5.5 Release Notes 中的示例:
[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
public class HelloService : Service
{
public object Any(Hello request) => new HelloResponse { Result = $"Hello, {request.Name}!" };
}
转到 /hello/World?format=html
为我提供了标准 HTML 报告,而不是我的模板。我按照另一个例子强制它使用模板....
public object Any(Hello request) =>
new PageResult(Request.GetPage("examples/hello")) {
Model = request.Name
};
...它总是 returns 我的模板,即使我指定 /hello/World?format=json
.
有什么方法可以让 ServiceStack + ScriptSharp 页面有类似 Razor 的视图选择,而且还支持不同的响应格式?
如果没有您想要实现的特定场景的详细信息,很难回答这样一个模糊的问题。
您可以通过多种方式returnSharp Pages:
- 直接作为内容页面请求时,例如
/dir/page
->/dir/page.html
- 使用 Page Based Routing,例如
/dir/1
->/dir/_id.html
- 作为 View Page 响应以 Request DTO 或 Response DTO 命名的服务,例如
/contacts/1
->/Views/GetContact.html
或/Views/GetContactResponse.html
Select 通过在自定义 HttpResult
:
public object Any(MyRequest request)
{
...
return new HttpResult(response)
{
View = "CustomPage", // -> /Views/CustomPage.html
//Template = "_custom-layout",
};
}
添加 [ClientCanSwapTemplates]
Request Filter 属性,让 View 和 Template 修改 QueryString,例如:?View=CustomPage&Template=_custom-layout
[ClientCanSwapTemplates]
public object Any(MyRequest request) => ...
通过 return 自定义 PageResult
:
public class CustomerServices : Service
{
public object Any(ViewCustomer request) =>
new PageResult(Request.GetPage("examples/customer")) {
Model = TemplateQueryData.GetCustomer(request.Id)
};
}
Note: That the
SharpPagesFeature
resolves pages using your cascadingAppHost.VirtualFileSources
. In .NET Core it's configured to use its WebRoot, e.g/wwwroot
.
对于 Sharp Pages return 其在多种内容类型中的响应:
as well to return the raw model in a variety of formats.
您需要使用 Sharp APIs which return
a value, e.g. /hello/_name/index.html:
{{ { result: `Hello, ${name}!` } | return }}
为了简洁地回答我自己的问题,@mythz 的第一个选项就是我所需要的。在我的 AppHost
中调用 Plugins.Add(new SharpPagesFeature())
后,我需要从我的服务方法 return HttpResult
:
public object Any(MyRequest request)
{
...
return new HttpResult(response)
{
View = "CustomPage", // -> /Views/CustomPage.html
//Template = "_custom-layout",
};
}