如果我希望能够通过/ 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这样的约束?
任何帮助都将不胜感激。
答案 0 :(得分:1)
您将为URI指定一个名称(使用config中的Named方法),并在要将该URI绑定到HttpOperation属性的方法上显示相同的名称,并将ForUriName属性设置为该值你注册了。