-
Notifications
You must be signed in to change notification settings - Fork 0
View and template selection
ServiceStack provides multiple ways to select the Razor View that will be used to render your services response with. First if the IHttpRequest.Items["View"]
has been set via any Global, Request, Response or Action Filter it will use that, otherwise the fallback convention is to use the view with the same name as the Request DTO followed finally by one with the Response DTO name.
To illustrate this, the below service is overloaded with different ways to select a Razor View, each are assigned a priority number starting from #1
:
[ClientCanSwapTemplates] // #4 Allow client selection with ?view=UserSpecified4
[DefaultView("DevSpecified3")] // #3
public class RockstarsService : Service
{
[DefaultView("DevSpecified2")] // #2
public object Get(Rockstars5 request) // #5
{
return new HttpResult(new RockstarsResponse6()) // #6
{
View = "DevSpecified1" // #1
}
}
}
The above service selects views that are mapped to the below Razor views. The folder structure is inconsequential so Views can be organized and nested in any number of folders, e.g:
/Views
/Any
/Nested
/Deep
DevSpecified1.cshtml // #1
DevSpecified2.cshtml // #2
DevSpecified3.cshtml // #3
UserSpecified4.cshtml // #4
Rockstars5.cshtml // #5
RockstarsResponse6.cshtml // #6
/Shared
_Layout.cshtml
The DevSpecified1.cshtml
would be selected first because it is last to set the IHttpRequest.Items["View"]
property that the Razor Format uses to select the view. You're also not limited to these options as you can easily set the same property in any of your own custom filters.
E.g. this is the entire source code of the ClientCanSwapTemplates Attribute which once enabled lets the client to specify the view via HTTP Header, QueryString, FormData or Cookies (in that order):
public class ClientCanSwapTemplatesAttribute : ResponseFilterAttribute
{
public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto)
{
req.Items["View"] = req.GetParam("View");
req.Items["Template"] = req.GetParam("Template");
}
}
The last filter to set req.Items["View"]
wins.
Although the above example only shows how to select the View (e.g. Page Body), the exact same rules applies to select the Layout template via the Template field also on the HttpResult and DefaultView attribute. If a template is not specified the default /Shared/_Layout.cshtml
is used.
- Why ServiceStack?
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
- Getting Started
- Reference
- Clients
- Formats
- View Engines 4. Razor & Markdown Razor
- Hosts
- Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in caching options
- Built-in profiling
- Messaging and Redis
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- Plugins
- Tests
- Other Languages
- Use Cases
- Performance
- How To
- Future