Academy PRO: ASP .NET Core MVC

23
ASP.NET Core MVC binary- studio.com

Transcript of Academy PRO: ASP .NET Core MVC

Page 1: Academy PRO: ASP .NET Core MVC

ASP.NET Core MVC

binary-studio.com

Page 2: Academy PRO: ASP .NET Core MVC

Contents

1. Model

2. View

3. Controller

Page 3: Academy PRO: ASP .NET Core MVC

Model

Page 4: Academy PRO: ASP .NET Core MVC

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

Page 5: Academy PRO: ASP .NET Core MVC

Additional typesIFormFile, IEnumerable<IFormFile>: One or more uploaded files that are part of the HTTP request.

CancelationToken: Used to cancel activity in asynchronous controllers.

Page 6: Academy PRO: ASP .NET Core MVC

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.

Page 7: Academy PRO: ASP .NET Core MVC

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));});

Page 8: Academy PRO: ASP .NET Core MVC

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)

Page 9: Academy PRO: ASP .NET Core MVC

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.

Page 10: Academy PRO: ASP .NET Core MVC

Model Validation

services.AddMvc(options => options.MaxModelValidationErrors = 50);

TryValidateModel(model);

Page 11: Academy PRO: ASP .NET Core MVC

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>

Page 12: Academy PRO: ASP .NET Core MVC

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);

}

Page 13: Academy PRO: ASP .NET Core MVC

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(), …)

Page 14: Academy PRO: ASP .NET Core MVC

View

Page 15: Academy PRO: ASP .NET Core MVC

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>

Page 16: Academy PRO: ASP .NET Core MVC

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

Page 17: Academy PRO: ASP .NET Core MVC

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>

Page 18: Academy PRO: ASP .NET Core MVC

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>

Page 19: Academy PRO: ASP .NET Core MVC

Dependency Injection services.AddTransient<StatisticsService>();

@inject StatisticsService StatsService

<h1>Total Items: @await StatsService.GetCount()</h1>

Page 20: Academy PRO: ASP .NET Core MVC

View Component<div>

@await Component.InvokeAsync("NameOfComponent", <anonymous type containing parameters>)</div>

public IActionResult IndexVC(){ return ViewComponent("MyList", new { count = 2 });}

Page 21: Academy PRO: ASP .NET Core MVC

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>

Page 22: Academy PRO: ASP .NET Core MVC

Controller

Page 23: Academy PRO: ASP .NET Core MVC

Routing

Areas

Filters

Error handling

Dependency Injection