Academy PRO: ASP .NET Core MVC
-
Upload
binary-studio -
Category
Software
-
view
125 -
download
0
Transcript of Academy PRO: ASP .NET Core MVC
ASP.NET Core MVC
binary-studio.com
Contents
1. Model
2. View
3. Controller
Model
Model Binding1. Form values: These are form values that go in the HTTP request
using the POST method. (including jQuery POST requests).
2. Route values: The set of route values provided by routing.
3. Query strings: The query string part of the URI. Model.IsValid
Additional typesIFormFile, IEnumerable<IFormFile>: One or more uploaded files that are part of the HTTP request.
CancelationToken: Used to cancel activity in asynchronous controllers.
Model Binding Attributes[BindRequired]: This attribute adds a model state error if binding cannot occur.
[BindNever]: Tells the model binder to never bind to this parameter.
[FromHeader], [FromQuery], [FromRoute], [FromForm]: Use these to specify the exact binding source you want to apply.
[FromServices]: This attribute uses dependency injection to bind parameters from services.
[FromBody]: Use the configured formatters to bind data from the request body. The formatter is selected based on content type of the request.
[ModelBinder]: Used to override the default model binder, binding source and name.
Input/Output FormattersThree input formatters:
– JsonInputFormatter – based on JSON.NET
– XmlSerializerInputFormatter – based on XmlSerializer (in the box, but not registered by default)
– XmlDataContractSerializerInputFormatter – based on DataContractSerializer
services.AddMvc()
.AddXmlSerializerFormatters();
services.AddMvc().Configure<MvcOptions>(options =>{ options.InputFormatters.RemoveAll(formatter => formatter.Instance.GetType() == typeof(XmlDataContractSerializerInputFormatter));});
Input/Output FormattersSix output formatters:
– JsonOutputFormatter – based on JSON.NET
– XmlSerializerOutputFormatter – based on XmlSerializer (in the box, but not registered by default)
– XmlDataContractSerializerOutputFormatter – based on DataContractSerializer
– TextPlainFormatter – used to force a string into a text/plain content type
– HttpNoContentOutputFormatter – used to force 204 status code for null action return
– HttpNotAcceptableOutputFormatter – used to force 406 status code if no appropriate formatter can be selected to handle the request (in the box, but not registered by default)
Model Validation[CreditCard]: Validates the property has a credit card format.
[Compare]: Validates two properties in a model match.
[EmailAddress]: Validates the property has an email format.
[Phone]: Validates the property has a telephone format.
[Range]: Validates the property value falls within the given range.
[RegularExpression]: Validates that the data matches the specified regular expression.
[Required]: Makes a property required.
[StringLength]: Validates that a string property has at most the given maximum length.
[Url]: Validates the property has a URL format.
Model Validation
services.AddMvc(options => options.MaxModelValidationErrors = 50);
TryValidateModel(model);
Client Model Validation<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script><script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"></script>
<div class="col-md-10"> <input asp-for="ReleaseDate" class="form-control" /> <span asp-validation-for="ReleaseDate" class="text-danger"></span></div>
<input class="form-control" type="datetime"
data-val="true" data-val-required="The ReleaseDate field is required."
id="ReleaseDate" name="ReleaseDate" value="" />
<span class="text-danger field-validation-valid"
data-valmsg-for="ReleaseDate" data-valmsg-replace="true"></span>
Remote Client Validationpublic class User{ [Remote(action: "VerifyEmail", controller: "Users")] public string Email { get; set; }} [AcceptVerbs("Get", "Post")]
public IActionResult VerifyEmail(string email)
{
if (!_userRepository.VerifyEmail(email))
{
return Json(data: $"Email {email} is already in use.");
}
return Json(data: true);
}
Response Data
IActionResult
return View(_myModel);
Json()
return Json(_authorRepository.List());
Content()
return Content("Some content.");
POCOs
[HttpGet("{alias}")]
public Author Get(string alias)
{
return _authorRepository.GetByAlias(alias);
}
Other (Ok(), NotFound(), …)
View
Tag [email protected]("FirstName", "First Name:", new {@class="caption"})
<label class="caption" asp-for="FirstName">First Name</label>
<label class="caption" for="FirstName">Name First</label>
Using Tag Helpers@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers // Add all tag helpers@removeTabHelper *, Microsoft.AspNetCore.Mvc.TagHelpers // Remove all tag helpers@tagHelperPrefix th:
<!span asp-validation-for="Email" class="text-danger"></!span> // Disable tag helper
<th:label asp-validation-for="Email" class="text-danger"></th:label> // Use prefix
Tag Helpers In Forms
<form asp-controller="Demo" asp-action="Register" method="post"> <!-- Input and Submit elements --></form>
<form method="post" action="/Demo/Register">
<!-- Input and Submit elements -->
<input name="__RequestVerificationToken" type="hidden" value="<removed for
brevity>" />
</form>
Custom Tag Helperpublic class EmailTagHelper : TagHelper{ private const string EmailDomain = "contoso.com"; public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { output.TagName = "a"; // Replaces <email> with <a> tag var content = await output.GetChildContentAsync(); var target = content.GetContent() + "@" + EmailDomain; output.Attributes.SetAttribute("href", "mailto:" + target); output.Content.SetContent(target); }}
<email>Support</email> <a href="mailto:[email protected]">[email protected]</a>
Dependency Injection services.AddTransient<StatisticsService>();
@inject StatisticsService StatsService
<h1>Total Items: @await StatsService.GetCount()</h1>
View Component<div>
@await Component.InvokeAsync("NameOfComponent", <anonymous type containing parameters>)</div>
public IActionResult IndexVC(){ return ViewComponent("MyList", new { count = 2 });}
View Componentpublic class MyListViewComponent : ViewComponent { public async Task<IViewComponentResult> InvokeAsync(int count) { var items = await GetItemsAsync(); return View(items.Take(count)); } private Task<List<string>> GetItemsAsync() { return Task.FromResult(new List<string>{“string1”, “string2”, “string3”}); } }
Views/Shared/Components/MyList
@model IEnumerable<string>
<h3>Priority Items</h3>
<ul>
@foreach (var item in Model)
{
<li>@item</li>
}
</ul>
Controller
Routing
Areas
Filters
Error handling
Dependency Injection