Skip to content

Commit

Permalink
Update live with current master (#4454)
Browse files Browse the repository at this point in the history
* Add PO localization doc

* More edits

* Updates TaskCache to Task

Since [TaskCache](dotnet/extensions#238) was removed this updates the samples to use `Task.CompletedTask` instead

* More edits

* More edits

* Change code snippet type from C# to CSHTML

* Add missing snippet types

* Add Required note to model validation topic

Fixes #1928

* MVC views overview update

Updates

Updates

Updates

Updates

Update

* React to feedback

* React to feedback

* Add support disclaimer

* UE pass on Publishing to IIS topic

* React to feedback

Minor update

* Move VS Tools for Docker doc to ASP.NET Core repo

* React to feedback

* React to feedback

* Drop literal_block comments and fix a few code blocks

* Fix demo code for AddAuthentication.AddCookie

In .Net Core 2.0 using the code as supplied results in an exception, the fix is to pass the scheme name when calling AddCookie.

Fails:
services.AddAuthentication("MyCookieAuthenticationScheme")
        .AddCookie

Works:
Fix demo code for services.AddAuthentication.AddCookie
services.AddAuthentication("MyCookieAuthenticationScheme")
        .AddCookie("MyCookieAuthenticationScheme",

* React to feedback

* Fix one

* React to feedback

* Convert title to sentence case

* Add missing param to AddCookie call

* React to feedback

* React to feedback

* Add RP tutorial step 'Uploading files' to TOC

* fix breadcrumb link to core

* Update model-binding.md

Fixing type `CancelationToken` -> `CancellationToken`

* minor clean up (#4431)

* Updates

* Fix a link

* Explain post delete method

Update

* Fix inadvertent uid change (typo)

* React to feedback

* React to feedback

* React to feedback

* Alter app cookie configuration code samples

* Update first-web-api-mac.md (#4441)

* Update web-api-vsc.md

* Remove dead project file (#4427)

* Updates to the intro: layouts, partials, VCs

* Update web-publishing-vs.md (#4447)

* Update web-publishing-vs.md

* Update web-publishing-vs.md

* Update identity-enable-qrcodes.md

* Update identity-enable-qrcodes.md

* Update identity-enable-qrcodes.md

* Remove extra space

* Minor edits

* Add TOC link to snapshot debugging doc

* Update index.md (#4443)

* Update index.md

* Update index.md
  • Loading branch information
Rick-Anderson committed Sep 29, 2017
1 parent 96ce03d commit b330159
Show file tree
Hide file tree
Showing 38 changed files with 500 additions and 224 deletions.
2 changes: 1 addition & 1 deletion aspnet/breadcrumb/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
topicHref: /aspnet/whitepapers/index
- name: ASP.NET Core
tocHref: /aspnet/core/
topicHref: /aspnet/core/intro
topicHref: /aspnet/core/index
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class MovieMetadata
[Required(ErrorMessage="Titles are required")]
public string Title { get; set; }

[Required(ErrorMessage="The Price is required.")]
[Range(5,100,ErrorMessage ="Movies cost between $5 and $100.")]
public decimal Price { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class Product
public string Description { get; set; }

[DisplayName("Price")]
[Required]
[RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
public decimal UnitPrice { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ public class Movie
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }

[Required(ErrorMessage = "Date is required")]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime ReleaseDate { get; set; }

[Required(ErrorMessage = "Genre must be specified")]
public string Genre { get; set; }

[Required(ErrorMessage = "Price Required")]
[Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal Price { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class Movie
[Required(ErrorMessage = "Genre must be specified")]
public string Genre { get; set; }

[Required(ErrorMessage = "Price Required")]
[Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
public decimal Price { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ public class Movie
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }

[Required(ErrorMessage = "Date is required")]
public DateTime ReleaseDate { get; set; }

[Required(ErrorMessage = "Genre must be specified")]
public string Genre { get; set; }

[Required(ErrorMessage = "Price Required")]
[Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
public decimal Price { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class Album
[StringLength(160, MinimumLength = 2)]
public string Title { get; set; }

[Required(ErrorMessage = "Price is required")]
[Range(0.01, 100.00, ErrorMessage = "Price must be between 0.01 and 100.00")]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class Album
[Required(ErrorMessage = "An Album Title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage = "Price is required")]
[Range(0.01, 100.00,
ErrorMessage = "Price must be between 0.01 and 100.00")]
public decimal Price { get; set; }
Expand Down
5 changes: 3 additions & 2 deletions aspnetcore/fundamentals/static-files.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Working with static files in ASP.NET Core
author: rick-anderson
description: Working with Static Files
description: Working with static files on ASP.NET Core
keywords: ASP.NET Core,static files,static assets,HTML,CSS,JavaScript
ms.author: riande
manager: wpickett
Expand All @@ -13,7 +13,8 @@ ms.prod: asp.net-core
uid: fundamentals/static-files
ms.custom: H1Hack27Feb2017
---
# Introduction to working with static files in ASP.NET Core

# Working with static files in ASP.NET Core

<a name=fundamentals-static-files></a>

Expand Down
4 changes: 2 additions & 2 deletions aspnetcore/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description:
keywords: ASP.NET Core,
ms.author: riande
manager: wpickett
ms.date: 08/03/2017
ms.date: 09/03/2017
ms.topic: article
ms.technology: aspnet
ms.prod: asp.net-core
Expand All @@ -17,7 +17,7 @@ By [Daniel Roth](https://github.com/danroth27), [Rick Anderson](https://twitter.

ASP.NET Core is a cross-platform, high-performance, [open-source](https://github.com/aspnet/home) framework for building modern, cloud-based, Internet-connected applications. With ASP.NET Core, you can:

* Build web apps and services, IoT apps, and mobile backends.
* Build web apps and services, [IoT](https://www.microsoft.com/en-us/internet-of-things/) apps, and mobile backends.
* Use your favorite development tools on Windows, macOS, and Linux.
* Deploy to the cloud or on-premises
* Run on [.NET Core or .NET Framework](https://docs.microsoft.com/dotnet/articles/standard/choosing-core-framework-server).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class NewIdeaModel
[Required]
public string Description { get; set; }

[Required]
[Range(1, 1000000)]
public int SessionId { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion aspnetcore/mvc/models/model-binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Additionally, there are some special data types that MVC must consider when perf

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

* `CancelationToken`: Used to cancel activity in asynchronous controllers.
* `CancellationToken`: Used to cancel activity in asynchronous controllers.

These types can be bound to action parameters or to properties on a class type.

Expand Down
38 changes: 25 additions & 13 deletions aspnetcore/mvc/models/validation.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Model validation in ASP.NET Core MVC
author: rick-anderson
description: Introduces model validation in ASP.NET Core MVC.
author: rachelappel
description: Learn about model validation in ASP.NET Core MVC.
keywords: ASP.NET Core,MVC,validation
ms.author: riande
manager: wpickett
Expand All @@ -10,7 +10,7 @@ ms.topic: article
ms.assetid: 3a8676dd-7ed8-4a05-bca2-44e288ab99ee
ms.technology: aspnet
ms.prod: asp.net-core
uid: mvc/models/validation
uid: mvc/models/validation
ms.custom: H1Hack27Feb2017
---
# Introduction to model validation in ASP.NET Core MVC
Expand All @@ -31,7 +31,7 @@ Validation attributes are a way to configure model validation so it's similar co

Below is an annotated `Movie` model from an app that stores information about movies and TV shows. Most of the properties are required and several string properties have length requirements. Additionally, there is a numeric range restriction in place for the `Price` property from 0 to $999.99, along with a custom validation attribute.

[!code-csharp[Main](validation/sample/Movie.cs?range=6-31)]
[!code-csharp[Main](validation/sample/Movie.cs?range=6-29)]

Simply reading through the model reveals the rules about data for this app, making it easier to maintain the code. Below are several popular built-in validation attributes:

Expand All @@ -57,6 +57,18 @@ MVC supports any attribute that derives from `ValidationAttribute` for validatio

There may be instances where you need more features than built-in attributes provide. For those times, you can create custom validation attributes by deriving from `ValidationAttribute` or changing your model to implement `IValidatableObject`.

## Notes on the use of the Required attribute

Non-nullable [value types](/dotnet/csharp/language-reference/keywords/value-types) (such as `decimal`, `int`, `float`, and `DateTime`) are inherently required and don't need the `Required` attribute. The app performs no server-side validation checks for non-nullable types that are marked `Required`.

MVC model binding, which isn't concerned with validation and validation attributes, rejects a form field submission containing a missing value or whitespace for a non-nullable type. In the absence of a `BindRequired` attribute on the target property, model binding ignores missing data for non-nullable types, where the form field is absent from the incoming form data.

The [BindRequired attribute](/aspnet/core/api/microsoft.aspnetcore.mvc.modelbinding.bindrequiredattribute) (also see [Customize model binding behavior with attributes](xref:mvc/models/model-binding#customize-model-binding-behavior-with-attributes)) is useful to ensure form data is complete. When applied to a property, the model binding system requires a value for that property. When applied to a type, the model binding system requires values for all of the properties of that type.

When you use a [Nullable\<T> type](/dotnet/csharp/programming-guide/nullable-types/) (for example, `decimal?` or `System.Nullable<decimal>`) and mark it `Required`, a server-side validation check is performed as if the property were a standard nullable type (for example, a `string`).

Client-side validation requires a value for a form field that corresponds to a model property that you've marked `Required` and for a non-nullable type property that you haven't marked `Required`. `Required` can be used to control the client-side validation error message.

## Model State

Model state represents validation errors in submitted HTML form values.
Expand Down Expand Up @@ -99,15 +111,15 @@ Client side validation is a great convenience for users. It saves time they woul

You must have a view with the proper JavaScript script references in place for client side validation to work as you see here.

[!code-html[Main](validation/sample/Views/Shared/_Layout.cshtml?range=37)]
[!code-cshtml[Main](validation/sample/Views/Shared/_Layout.cshtml?range=37)]

[!code-html[Main](validation/sample/Views/Shared/_ValidationScriptsPartial.cshtml)]
[!code-cshtml[Main](validation/sample/Views/Shared/_ValidationScriptsPartial.cshtml)]

MVC uses validation attributes in addition to type metadata from model properties to validate data and display any error messages using JavaScript. When you use MVC to render form elements from a model using [Tag Helpers](xref:mvc/views/tag-helpers/intro) or [HTML helpers](xref:mvc/views/overview) it will add HTML 5 [data- attributes](http://w3c.github.io/html/dom.html#embedding-custom-non-visible-data-with-the-data-attributes) in the form elements that need validation, as shown below. MVC generates the `data-` attributes for both built-in and custom attributes. You can display validation errors on the client using the relevant tag helpers as shown here:

[!code-html[Main](validation/sample/Views/Movies/Create.cshtml?highlight=4,5&range=19-25)]
[!code-cshtml[Main](validation/sample/Views/Movies/Create.cshtml?highlight=4,5&range=19-25)]

The tag helpers above render the HTML below. Notice that the `data-` attributes in the HTML output correspond to the validation attributes for the `ReleaseDate` property. The `data-val-required` attribute below contains an error message to display if the user doesn't fill in the release date field, and that message displays in the accompanying `<span>` element.
The tag helpers above render the HTML below. Notice that the `data-` attributes in the HTML output correspond to the validation attributes for the `ReleaseDate` property. The `data-val-required` attribute below contains an error message to display if the user doesn't fill in the release date field, and that message displays in the accompanying **\<span>** element.

```html
<form action="/Movies/Create" method="post">
Expand Down Expand Up @@ -142,11 +154,11 @@ Attributes that implement this interface can add HTML attributes to generated fi

```html
<input class="form-control" type="datetime"
data-val="true"
data-val-classicmovie="Classic movies must have a release year earlier than 1960."
data-val-classicmovie-year="1960"
data-val-required="The ReleaseDate field is required."
id="ReleaseDate" name="ReleaseDate" value="" />
data-val="true"
data-val-classicmovie="Classic movies must have a release year earlier than 1960."
data-val-classicmovie-year="1960"
data-val-required="The ReleaseDate field is required."
id="ReleaseDate" name="ReleaseDate" value="" />
```

Unobtrusive validation uses the data in the `data-` attributes to display error messages. However, jQuery doesn't know about rules or messages until you add them to jQuery's `validator` object. This is shown in the example below that adds a method named `classicmovie` containing custom client validation code to the jQuery `validator` object.
Expand Down
2 changes: 0 additions & 2 deletions aspnetcore/mvc/models/validation/sample/Movie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class Movie
[StringLength(100)]
public string Title { get; set; }

[Required]
[ClassicMovie(1960)]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
Expand All @@ -20,7 +19,6 @@ public class Movie
[StringLength(1000)]
public string Description { get; set; }

[Required]
[Range(0, 999.99)]
public decimal Price { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class MovieIValidatable : IValidatableObject
[StringLength(1000)]
public string Description { get; set; }

[Required]
[Range(0, 999.99)]
public decimal Price { get; set; }

Expand Down
36 changes: 30 additions & 6 deletions aspnetcore/mvc/razor-pages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,9 @@ The code behind *Index.cshtml.cs* file:

The *Index.cshtml* file contains the following markup to create an edit link for each contact:

```cshtml
<a asp-page="./Edit" asp-route-id="@contact.Id">edit</a>
```
[!code-cshtml[main](index/sample/RazorPagesContacts/Pages/Index.cshtml?range=21)]

The [Anchor Tag Helper](xref:mvc/views/tag-helpers/builtin-th/anchor-tag-helper)
used the [asp-route-{value}](xref:mvc/views/tag-helpers/builtin-th/anchor-tag-helper#route)
attribute to generate a link to the Edit page. The link contains route data with the contact ID. For example, `http://localhost:5000/Edit/1`.
The [Anchor Tag Helper](xref:mvc/views/tag-helpers/builtin-th/anchor-tag-helper) used the [asp-route-{value}](xref:mvc/views/tag-helpers/builtin-th/anchor-tag-helper#route) attribute to generate a link to the Edit page. The link contains route data with the contact ID. For example, `http://localhost:5000/Edit/1`.

The *Pages/Edit.cshtml* file:

Expand All @@ -171,6 +167,34 @@ The *Pages/Edit.cshtml.cs* file:

[!code-cs[main](index/sample/RazorPagesContacts/Pages/Edit.cshtml.cs)]

The *Index.cshtml* file also contains markup to create a delete button for each customer contact:

[!code-cshtml[main](index/sample/RazorPagesContacts/Pages/Index.cshtml?range=22-23)]

When the delete button is rendered in HTML, its `formaction` includes parameters for:

* The customer contact ID specified by the `asp-route-id` attribute.
* The `handler` specified by the `asp-page-handler` attribute.

Here is an example of a rendered delete button with a customer contact ID of `1`:

```html
<button type="submit" formaction="/?id=1&amp;handler=delete">delete</button>
```

When the button is selected, a form `POST` request is sent to the server. By convention, the name of the handler method is selected based the value of the `handler` parameter according to the scheme `OnPost[handler]Async`.

Because the `handler` is `delete` in this example, the `OnPostDeleteAsync` handler method is used to process the `POST` request. If the `asp-page-handler` is set to a different value, such as `remove`, a page handler method with the name `OnPostRemoveAsync` is selected.

[!code-cs[main](index/sample/RazorPagesContacts/Pages/Index.cshtml.cs?range=26-37)]

The `OnPostDeleteAsync` method:

* Accepts the `id` from the query string.
* Queries the database for the customer contact with `FindAsync`.
* If the customer contact is found, they're removed from the list of customer contacts. The database is updated.
* Calls `RedirectToPage` to redirect to the root Index page (`/Index`).

<a name="xsrf"></a>

## XSRF/CSRF and Razor Pages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void Main(string[] args)

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
#if Debug
#if Debug
.UseStartup<StartupDebug>()
#else
.UseStartup<Startup>()
Expand Down

0 comments on commit b330159

Please sign in to comment.