View and template selection
This page has moved to docs.servicestack.net
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 Client can select 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.
Razor Views are now debuggable for Debug builds by default, it can also be explicitly specified on:
Plugins.Add(new RazorFormat {
IncludeDebugInformation = true,
CompileFilter = compileParams => ...
})
The CompileFilter
is an advanced option that lets modify the CompilerParameters
used by the C# CodeDom provider to compile the Razor Views if needed.
- Why ServiceStack?
- Important role of DTOs
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
-
Getting Started
-
Designing APIs
-
Reference
-
Clients
-
Formats
-
View Engines 4. Razor & Markdown Razor
-
Hosts
-
Security
-
Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in profiling
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Dump Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- MVC Integration
- ServiceStack Integration
- Embedded Native Desktop Apps
- Auto Batched Requests
- Versioning
- Multitenancy
-
Caching
-
HTTP Caching 1. CacheResponse Attribute 2. Cache Aware Clients
-
Auto Query
-
AutoQuery Data 1. AutoQuery Memory 2. AutoQuery Service 3. AutoQuery DynamoDB
-
Server Events
-
Service Gateway
-
Encrypted Messaging
-
Plugins
-
Tests
-
ServiceStackVS
-
Other Languages
-
Amazon Web Services
-
Deployment
-
Install 3rd Party Products
-
Use Cases
-
Performance
-
Other Products
-
Future