Reference:Requires
Contents |
Overview
The Requires attribute indicates that execution of the marked controller cannot occur until the marked value has been supplied onto the context.
Usage
This attribute is specified for Property and Field level usage. Controller execution order, while being significantly influenced by the rules defined through Bind attributes, are governed by the set of Provides/Requires/DependsOn attributes. Controllers which provide a value v will run before controllers that Require or DependOn v. The order enforced here supersedes the order enforced through the Bind attribute.
- Be Careful
- If the required value is not supplied by any controller in the execution path, a run-time exception will be thrown. While if we use DependsOn attribute, the controller will be invoked with
nullsupplied to the marked field.
Name
The name property can be used to redefine the resource name made public by this attribute. The value defaults to the name of the property or field marked.
Example
The following code demonstrates how to ensure that a controller does not execute if a required value is missing. The AdSvc controller is used to return an xml representation of the supplied value. If the supplied value is missing, the controller has no way of recovering, therefore the value is deemed to be Required. In this configuration, if a request comes in for /content/ad/somead/asxml, the Ad controller will execute first, as it is a provider of ad, followed by the AdSvc controller, which, among other things, will override the template that will be used to render the output.
[Bind("/content/ad/{adId}/asxml")] public class AdSvc : AbstractController { [Request, Requires] protected Posting ad; [Request] protected Posting target; public override void DoProcessRequest(IExecutionContext context) { target = ad; context.RenderWith("/Templates/xml.django"); } } [Bind("/content/ad/{adId}")] public class Ad : AbstractController { [Request] protected string contentType; [Request] protected Posting ad; protected string adId; public override void DoProcessRequest(IExecutionContext context) { ad = PostingHelper.Instance.LoadPosting(null, adId); contentType = ContentType.Resume.Equals(ContentTypeHelper.Instance.GetLastDefault()) ? "resume" : "ad"; context.RenderWith("/Templates/Ad/view.django"); } }