我在web api中做了一些代码,我想在模型类中列出所有变量并显示它们
这是web api的代码:
//Method to list all the variables from the class Hello
[HttpGet]
[Route("api/listOfVariables")]
public IEnumerable<String> listOfVariables()
{
return typeof(Hello).GetFields()
.Select(field => field.Name)
.ToList();
}
模型类
public class Hello
{
public int HelloId { get; set; }
public string name { get; set; }
}
}
和web api配置:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
当我使用以下网址时: http://localhost:1861/api/listOfVariables
我得到这个信息:
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
有人能帮助我吗?我是.net
的新手答案 0 :(得分:1)
Hello
课程没有任何字段,因此您所看到的是一个空列表。该类具有属性。您可以使用GetProperties()
来获取这些内容。
举例说明:
class Hello
{
public int HelloId; // field
}
class Hello
{
public int HelloId { get; set; } // property
}