Professional Documents
Culture Documents
NET
asp-for
The asp-for attribute is used to connect an <!-- In .cshtml file -->
<input> element to a page model property. Enter a username: <input asp-
The above example would be connected to a
Username property in the page model: for="Username" />
Enter a username:
<input type="text" id="Username
About.cshtml is available at
https://localhost:8000/Abou
t
Codecademy.cshtml is available at
https://localhost:8000/Busin
esses/Codecademy
Index.cshtml is available at
https://localhost:8000/Inde
x OR https://localhost:8000
public IndexModel(UserContext
context)
{
_context = context;
}
Model Binding
In model binding, a page model retrieves data from public class IndexModel : PageModel
an HTTP request, converts the data to .NET types,
{
and updates the corresponding model properties. It
is enabled with the [BindProperty] [BindProperty]
attribute. public string Username { get; set; }
[BindProperty]
public bool IsActive { get; set; }
// Example POST
// https://localhost:8000?
username=muhammad&IsActive=true
public void OnPost()
{
// Username is "muhammad"
// IsActive is true
}
}
@page "Me"
@page "{id:int}"
@page "{size?}"
@page "{song:int?}"
asp-route-{value}
The asp-route-{value} attribute is used in <a asp-page="About" asp-route-
<a> elements to add additional information to a
name="Joanne">About Joanne</a>
URL route.
{value} typically matches a property in
a page model.
The provided value will be added as a route
segment or a query string, depending on
how the route is de!ned.
If the above <a> tag is in a .cshtml !le, it would be
rendered as this HTML:
<a href="localhost:8000/About?name=Joanne
<a href="localhost:8000/About/Joanne
Default Responses
In page models, a handler method with no public class IndexModel : PageModel
return statement will respond to HTTP requests
{
by sending back the associated page.
In the above example, IndexModel is // Sends Index.cshtml
associated with Index.cshtml. Neither OnGet() public void OnGet()
nor OnPostAsync() have return { }
statements, so they both return Index.cshtml.
// Sends Index.cshtml
public void OnPost()
{ }
}
Page()
To return the view page associated with a page public class AboutModel : PageModel
model, use Page() in the page model’s handler
{
methods.
This happens implicitly if the handler method // Sends About.cshtml
has a void return type public IActionResult OnGet()
If a handler method calls Page() , its {
return type is typically IActionResult
return Page();
or Task<IActionResult> (although
others exist). }
}
RedirectToPage()
To redirect users to a di"erent Razor page within the public class IndexModel : PageModel
application, use RedirectToPage() in the
{
page model’s handler methods.
If a handler method calls // Sends Privacy.cshtml
RedirectToPage() , its return type is public IActionResult OnPost()
typically IActionResult or {
Task<IActionResult> (although return RedirectToPage("./Privacy");
others exist).
}
The string argument passed to this method is
a !le path. "/Index" is a relative path
}
and "./Index" is an absolute path.
NotFound()
To send a “Status 404 Not Found” response, use public class EditModel : PageModel
NotFound() in the page model’s handler
{
methods.
If a handler method calls NotFound() , public async Task<IActionResult>
its return type is typically OnGetAsync(int? id)
IActionResult or {
Task<IActionResult> (although
if (id == null)
others exist).
{
return NotFound();
}
return Page();
}
}