开发者

OpenRasta Route Constraints

If I want to be able to get a person by /Person/1 and /Person/Blair How can i do this.

Configuration Class:

public class RastaConfiguration : IConfigurationSource
{
    public void Configure()
    {
        using (OpenRastaConfiguration.Manual)
        {

            ResourceSpace.Has.ResourcesOfType<List<Subject>>()
                .AtUri("/Subject")
                .HandledBy<SubjectHandler>()
                .TranscodedBy<JsonDataContractCodec>();

            ResourceSpace.Has.ResourcesOfType<List<Person>>()
                .AtUri("/Person").And.AtUri("/Person/{name}")
                .HandledBy<PersonHandler>()
                .TranscodedBy<JsonDataContractCodec>();

            ResourceSpace.Has.ResourcesOfType<Person>()
                .AtUri("/Person/{id}")
                .HandledBy<PersonHandler>()
                .TranscodedBy<JsonDataContractCodec>();
        }
    }
}

and handler

 public class PersonHandler
{

    private static readonly List<Person> People = new List<Person>
                                             {
                                                 new Person {Id = 1, Name = "Blair Davidson", Subjects = new List<Subject>
                    开发者_如何转开发                                                                              {
                                                                                                      new Subject{ Name = "Maths"}
                                                                                                  }},
                                                 new Person {Id = 2, Name = "Esther Hew", Subjects = new List<Subject>
                                                                                                  {
                                                                                                      new Subject{ Name = "Chinese"}
                                                                                                  }}
                                             };

    public IEnumerable<Person> Get()
    {
        return People;
    }

    public IEnumerable<Person> GetByName(string name)
    {
        return People.Where(x => x.Name.Contains(name)).ToList();
    }

    public Person GetById(int id)
    {
        return People.SingleOrDefault(x => x.Id == id);
    }

    public OperationResult Post(Person person)
    {
        People.Add(person);

        return new OperationResult.Created
                   {
                       ResponseResource = person
                   };
    }


}

Is there a way to use constraints like asp.net mvc?

Any help would be appreciated.


You'd give a name to the URI (using the Named method in config), and have that same name present on the method you want to bind that URI to with the HttpOperation attribute, with the ForUriName property set to the value you had in registration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜