Building RESTful Web Services with .NET Core
上QQ阅读APP看书,第一时间看更新

What's cooking here?

Notice the URL, localhost:57571/api/values, which sends the request to ValuesController because the route defined over the controller is [Route("api/[controller]")]. By convention, the controller name is always appended with the text Controller. Thus api/values hits ValuesController.

Now the question is, how it returns value1 and value2. That is because we directly accessed the URL through the browser, which ultimately sent a GET request to the controller. As the controller already has a Get method, it got executed. The Get method is as follows:

// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

This method returns an array of strings, which is printed in the browser. For understanding, the URL format is already there above the method (api/values).