Actions must return a revel.Result, which handles the HTTP response generation and adheres to the simple interface:
revel.Controller provides a few methods to produce different results:
- `RenderJson` is now `RenderJSON`
- `RenderJsonP` is now `RenderJSONP`
- `RenderXml` is now `RenderXML`
- Render(), RenderTemplate()
- render a template, passing arguments.
- RenderJSON(), RenderXML()
- serialize a structure to json or xml.
- return a plaintext response.
- redirect to another action or URL
- return a file, generally to be downloaded as an attachment.
- return a 500 response that renders the errors/500.html template.
- return a 404 response that renders the errors/404.html template.
- return a stub response (500)
Additionally, the developer may define a result with CustomResult and return that.
Setting the Status Code / Content Type
Each built-in Result has a default
HTTP Status Code and
Content Type. To override
those defaults, simply set those properties on the response:
You can override the default status code by setting one yourself:
Called within an action (e.g. “Controller.Action”), Controller.Render() does two things:
- Adds all arguments to the controller’s
ViewArgs, using their local identifier as the key.
- Executes the template “views/Controller/Action.html”, passing in the controller’s
ViewArgsas the data map.
If unsuccessful (e.g. it could not find the template), an ErrorResult is returned instead.
This allows the developer to write:
and to use
myValue in their template. This is usually more convenient than
constructing an explicit map, since in many cases the data will need to be
handled as a local variable anyway.
RenderJSON() / RenderXML()
- A helper function for generating HTTP redirects.
- It may be used in two ways and both return a
302 Temporary RedirectHTTP status code.
Redirect to an action with no arguments:
- This form is useful as it provides a degree of type safety and independence from the routing and generates the URL automatically.
Redirect to a formatted string:
- This form is necessary to pass arguments.
- It returns a
302 Temporary Redirectstatus code.
Below is a simple example of creating a custom revel.Result.
Create this type:
Then use it in an action: