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

Ra p kellner built in th #2785

Merged
merged 16 commits into from
Jul 8, 2017
Merged
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
134 changes: 104 additions & 30 deletions aspnetcore/mvc/views/tag-helpers/built-in/AnchorTagHelper.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
---
title: Anchor Tag Helper | Microsoft Docs
author: Peter Kellner
author: pkellner
description: Shows how to work with Anchor Tag Helper
keywords: ASP.NET Core,tag helper
ms.author: riande
Expand All @@ -12,15 +12,13 @@ ms.technology: aspnet
ms.prod: aspnet-core
uid: mvc/views/tag-helpers/builtin-th/AnchorTagHelper
---

# Anchor Tag Helper

By [Peter Kellner](http://peterkellner.net)

The Anchor Tag Helper enhances the HTML anchor (`<a ... ></a>`) tag by adding new attributes. The link generated (on the `href` tag) is created using the new attributes. That URL can include an optional protocol such as https.

The Anchor Tag Helper enhances the html anchor (`<a ... ></a>`) tag. A new set of attributes are defined that work with the anchor tag. The link generated (on the `href` tag) is based on a combination of these new attributes that work together to form the final URL. That URL can include an optional protocol such as https.

The speaker controller used in attribute definitions below is shown here.
The speaker controller below is used in samples in this document.

<br/>
**SpeakerController.cs**
Expand All @@ -34,51 +32,126 @@ The speaker controller used in attribute definitions below is shown here.

### asp-controller

`asp-controller` is used to associate which controller will be used to generate the final URL. The only valid choices are controllers that exist in the current project. To get to a list of all speakers specifying `asp-controller="Speaker"` is required. If only the `asp-controller` and no `asp-action` is specified, the default asp-action will be the name of the the controller method calling this view page.
`asp-controller` is used to associate which controller will be used to generate the URL. The controllers specified must exist in the current project. The following code lists all speakers:

```
<a asp-controller="Speaker" asp-action="Index" >All Speakers</a>
```

The generated URL will be:

```
<a href="/Speaker">All Speakers</a>
```

If the `asp-controller` is specified and `asp-action` is not, the default `asp-action` will be the default controller method of the currently executing view. That is, in the above example, if `asp-action` is left out, and this Anchor Tag Helper is generated from *HomeController*'s `Index` view (**/Home**), the generated markup will be:


```html
<a href="/Home">All Speakers</a>
```

- - -

### asp-action

`asp-action` is the name of the method in the controller that will be included in the final URL. That is, in the example, if the route to the Speaker Detail page is wanted, then the attribute should be set to `asp-action=Detail`. You should always set `asp-controller` when specifying `asp-action`. If no `asp-action` is specified then the default `asp-controller` will be the current executing controller.
`asp-action` is the name of the action method in the controller that will be included in the generated `href`. For example, the following code set the generated `href` to point to the speaker detail page:

```html
<a asp-controller="Speaker" asp-action="Detail" >Speaker Detail</a>
```

The generated URL will be:

```html
<a href="/Speaker/Detail">Speaker Detail</a>
```

If no `asp-controller` attribute is specified, the default controller calling the view executing the current view will be used.

If the attribute `asp-action` is `Index`, then no action is appended to the URL, leading to the default `Index` method being called. The action specified (or defaulted), must exist in the controller referenced in `asp-controller`.

- - -

### asp-route-{value}

`asp-route-` is a wild card route prefix. Any value you put after the trailing dash will be interpreted as the parameter to pass into the route. For example, if a tag is created as follows:
`asp-route-` is a wild card route prefix. Any value you put after the trailing dash will be interpreted as a potential route parameter. If a default route is not found, this route prefix will be appended to the generated href as a request parameter and value. Otherwise it will be substituted in the route template.

Assuming you have a controller method defined as follows:

```csharp
public IActionResult AnchorTagHelper(string id)
{
var speaker = new SpeakerData()
{
SpeakerId = 12
};
return View(viewName, speaker);
}
```

And have the default route template defined in your **Startup.cs** as follows:

```csharp
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

```

`<a asp-controller="Speaker" asp-action="Detail" asp-route-id-="11">Speaker 11</a>`
The **cshtml** file that contains the Anchor Tag Helper necessary to use the **speaker** model parameter passed in from the controller to the view is as follows:

the `href` generated will be
```html
@model SpeakerData
<!DOCTYPE html>
<html><body>
<a asp-controller='Speaker' asp-action='Detail' asp-route-id=@Model.SpeakerId>SpeakerId: @Model.SpeakerId</a>
<body></html>
```

`<a href="/Speaker/11">Speaker 11</a>`
The generated HTML will then be as follows because **id** was found in the default route.

This is because a route was found that matched a single parameter "id" in the ```SpeakerController``` method ```Detail```. If there was no parameter match, say for example you created the tag helper
```html
<a href='/Speaker/Detail/12'>SpeakerId: 12</a>
```

`<a asp-controller="Speaker" asp-action="Detail" asp-route-name-="Ronald">Ronald</a>`
If the route prefix is not part of the routing template found, which is the case with the following **cshtml** file:

you would get generated the html
```html
@model SpeakerData
<!DOCTYPE html>
<html><body>
<a asp-controller='Speaker' asp-action='Detail' asp-route-speakerid=@Model.SpeakerId>SpeakerId: @Model.SpeakerId</a>
<body></html>
```

`<a href="/Speaker/Detail?Name=Ronald">Ronald</a>`
The generated HTML will then be as follows because **speakerid** was not found in the route matched:

This is because there was no route found that matched a controller that had a method named `Detail` with one string parameter titled `name`.

```html
<a href='/Speaker/Detail?speakerid=12'>SpeakerId: 12</a>
```

If either `asp-controller` or `asp-action` are not specified, then the same default processing is followed as is in the `asp-route` attribute.

- - -

### asp-route

`asp-route` provides a way to create a URL that links directly to a named route. Using routing attributes, a route can be named as shown in the `SpeakerController` and used in its `Evaluations` method.

`Name = "speakerevals"` tells the Anchor Tag Helper to generate a route directly to that controller method using the URL `/Speaker/Evaluations`. If `asp-controller` or `asp-action` is specified in addition to `asp-route`, the route generated may not be what you expect. `asp-route` should not be used with either of the attributes `asp-controller` or `asp-action` to avoid a route conflict.
`Name = "speakerevals"` tells the Anchor Tag Helper to generate a route directly to that controller method using the URL `/Speaker/Evaluations`. If `asp-controller` or `asp-action` is specified in addition to `asp-route`, the route generated may not be what you expect. `asp-route` should not be used with either of the attributes `asp-controller` or `asp-action` to avoid a route conflict.

- - -

### asp-all-route-data

`asp-all-route-data` allows creating on a .NET context (that is, the running C# associated with your Razor view) a dictionary of key value pairs where the key is the parameter name and the value is the value associated with that key.
`asp-all-route-data` allows creating a dictionary of key value pairs where the key is the parameter name and the value is the value associated with that key.

As the example below shows, an inline dictionary is created and the data is passed to the razor view. The data could also be passed in with your model to keep the Razor view simpler.
As the example below shows, an inline dictionary is created and the data is passed to the razor view. As an alternative, the data could also be passed in with your model.
Copy link
Member

Choose a reason for hiding this comment

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

The "r" in "razor" should be capitalized, since it's a proper noun.


```
@{
Expand All @@ -93,39 +166,40 @@ As the example below shows, an inline dictionary is created and the data is pass
asp-all-route-data="dict">SpeakerEvals</a>
```

The code that this generates looks as follows:
The code above generates the following HTML:

```
http://localhost/Speaker/EvaluationsCurrent?speakerId=11&currentYear=true
```

When the link is clicked, this will call the controller method `EvaluationsCurrent` because that controller has two string parameters that match what has been created from the `asp-all-route-data` dictionary.
When the link is clicked, the controller method `EvaluationsCurrent` is called. It is called because that controller has two string parameters that match what has been created from the `asp-all-route-data` dictionary.

If any keys in the dictionary match route parameters, those values will be substituted in the route as appropriate and the other non-matching values will be generated as request parameters.

- - -

### asp-fragment

`asp-fragment` defines a URL fragment to append to the URL. The Anchor Tag Helper will add the hash character (#) automatically. If you create a tag:
`asp-fragment` defines a URL fragment to append to the URL. The Anchor Tag Helper will add the hash character (#). If you create a tag:

```
<a asp-action="Evaluations" asp-controller="Speaker"
asp-fragment="SpeakerEvaluations">About Speaker Evals</a>
```

The generated URL will be

The generated URL will be:

```
http://localhost/Speaker/Evaluations#SpeakerEvaluations
```

Hash tags are useful when doing client side applications. They can be used for easy marking and searching in JavaScript for example.
Hash tags are useful when building client-side applications. They can be used for easy marking and searching in JavaScript, for example.

- - -

### asp-area

`asp-area` sets the area name that ASP.NET Core uses to set the appropriate route. Below are examples of how the area attribute causes a remapping of routes. Setting `asp-area` to Blogs prefixes the directory Areas/Blogs to the routes of the associated controllers and views for this anchor tag..
`asp-area` sets the area name that ASP.NET Core uses to set the appropriate route. Below are examples of how the area attribute causes a remapping of routes. Setting `asp-area` to Blogs prefixes the directory `Areas/Blogs` to the routes of the associated controllers and views for this anchor tag.

* Project name

Expand Down Expand Up @@ -170,19 +244,19 @@ The generated HTML will include the areas segment and will be as follows:

### asp-protocol

The `asp-protocol` is for specifying a particular protocol (such as `https`) in your URL. An example Anchor Tag Helper that includes the protocol will look as follows.
The `asp-protocol` is for specifying a protocol (such as `https`) in your URL. An example Anchor Tag Helper that includes the protocol will look as follows:

```<a asp-protocol="https" asp-action="About" asp-controller="Home">About</a>```

and will generate HTML as follows.
and will generate HTML as follows:

```<a href="https://localhost/Home/About">About</a>```

The domain in the example is localhost, but the Anchor Tag Helper uses the website's public domain when generating the URL.

- - -

## Additional Resources
## Additional resources

* [Areas](xref:mvc/controllers/areas)

Expand Down
Loading