如果我希望能够通过 /Person/1 和 /Person/Blair 找到一个人,我该怎么做。
配置类:
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>();
}
}
}
和处理程序
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
};
}
}
有没有办法使用像 asp.net mvc 这样的约束?
任何帮助,将不胜感激。