Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getting-started.md #296

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions Odata-docs/webapi-8/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,10 @@ namespace Lab01.Controllers
}

[EnableQuery]
public ActionResult<Customer> Get([FromRoute] int key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason for this change. $expand works okay even when the return type is ActionResult<Customer>.

public SingleResult<Customer> Get([FromRoute] int key)
{
var item = customers.SingleOrDefault(d => d.Id.Equals(key));

if (item == null)
{
return NotFound();
}

return Ok(item);
var item = customers.AsQueryable().Where(c => c.Id == key);
return SingleResult.Create(item);
}
}
}
Expand Down
24 changes: 6 additions & 18 deletions Odata-docs/webapi-8/tutorials/basic-crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,20 +376,14 @@ The following JSON payload shows the expected response:
## Request a single entity
To support this request, we add a controller action named `Get` (or `GetCustomer`) to the `CustomersController` class. The action should accept a single parameter named `key` of type `int` - same type as the entity's key property:
```csharp
public ActionResult<Customer> Get([FromRoute] int key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason for this change. $expand works okay even when the return type is ActionResult<Customer>.

public SingleResult<Customer> Get([FromRoute] int key)
{
var customer = db.Customers.SingleOrDefault(d => d.Id == key);

if (customer == null)
{
return NotFound();
}

return Ok(customer);
var item = customers.AsQueryable().Where(c => c.Id == key);
return SingleResult.Create(item);
}
```

In the above block of code, we're using `LINQ` to write an expression to retrieve a entity with the specified key from the `Customers` table. We return a status 404 `NotFound` if we don't find any match; otherwise we return the matched entity.
The SingleResult.Create method return a status 404 `NotFound` if we don't find any match; otherwise we return the matched entity.

The `FromRoute` attribute is only added for documentation purposes to show that the parameter should be bound using route-data from the current request - it is neither necessary nor mandatory.

Expand All @@ -398,14 +392,8 @@ Note that for the built-in routing conventions to route the request successfully
[HttpGet("odata/Customers({id})")]
public ActionResult Get([FromRoute] int id)
{
var customer = db.Customers.SingleOrDefault(d => d.Id == id);

if (customer == null)
{
return NotFound();
}

return Ok(customer);
var item = customers.AsQueryable().Where(c => c.Id == id);
return SingleResult.Create(item);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason for this change. $expand works okay even when the return type is ActionResult.

}
```

Expand Down