|
1 | 1 | --- |
2 | 2 | title: Anchor Tag Helper in ASP.NET Core |
| 3 | +ai-usage: ai-assisted |
3 | 4 | author: pkellner |
4 | 5 | description: Discover the ASP.NET Core Anchor Tag Helper attributes and the role each attribute plays in extending behavior of the HTML anchor tag. |
| 6 | +monikerRange: '>= aspnetcore-6.0' |
5 | 7 | ms.author: wpickett |
6 | 8 | ms.custom: mvc |
7 | | -ms.date: 10/13/2019 |
| 9 | +ms.date: 05/08/2026 |
8 | 10 | uid: mvc/views/tag-helpers/builtin-th/anchor-tag-helper |
9 | 11 | --- |
| 12 | + |
10 | 13 | # Anchor Tag Helper in ASP.NET Core |
11 | 14 |
|
12 | 15 | By [Peter Kellner](https://peterkellner.net) and [Scott Addie](https://github.com/scottaddie) |
@@ -65,9 +68,14 @@ Consider the following controller action: |
65 | 68 |
|
66 | 69 | [!code-csharp[](samples/TagHelpersBuiltIn/Controllers/SpeakerController.cs?name=snippet_SpeakerDetailAction)] |
67 | 70 |
|
68 | | -With a default route template defined in *Startup.Configure*: |
69 | 71 |
|
70 | | -[!code-csharp[](samples/TagHelpersBuiltIn/Startup.cs?name=snippet_UseMvc&highlight=8-10)] |
| 72 | +With a default route template defined in `Program.cs`: |
| 73 | + |
| 74 | +```csharp |
| 75 | +app.MapControllerRoute( |
| 76 | + name: "default", |
| 77 | + pattern: "{controller=Home}/{action=Index}/{id?}"); |
| 78 | +``` |
71 | 79 |
|
72 | 80 | The MVC view uses the model, provided by the action, as follows: |
73 | 81 |
|
@@ -193,13 +201,9 @@ The generated HTML: |
193 | 201 | <a href="/Sessions">View Sessions</a> |
194 | 202 | ``` |
195 | 203 |
|
| 204 | + |
196 | 205 | > [!TIP] |
197 | | -> To support areas in a Razor Pages app, do one of the following in `Startup.ConfigureServices`: |
198 | | -> |
199 | | -> * Set the [compatibility version](xref:mvc/compatibility-version) to 2.1 or later. |
200 | | -> * Set the <xref:Microsoft.AspNetCore.Mvc.RazorPages.RazorPagesOptions.AllowAreas%2A?displayProperty=nameWithType> property to `true`: |
201 | | -> |
202 | | -> [!code-csharp[](samples/TagHelpersBuiltIn/Startup.cs?name=snippet_AllowAreas)] |
| 206 | +> To support areas in a Razor Pages app, ensure that area routing is enabled in your app's routing configuration. Use the minimal hosting model and configure area routes in *Program.cs* as needed. |
203 | 207 |
|
204 | 208 | #### Usage in MVC |
205 | 209 |
|
@@ -228,10 +232,15 @@ The generated HTML: |
228 | 232 | <a href="/Blogs/Home/AboutBlog">About Blog</a> |
229 | 233 | ``` |
230 | 234 |
|
| 235 | + |
231 | 236 | > [!TIP] |
232 | | -> To support areas in an MVC app, the route template must include a reference to the area, if it exists. That template is represented by the second parameter of the `routes.MapRoute` method call in *Startup.Configure*: |
| 237 | +> To support areas in an MVC app, the route template must include a reference to the area, if it exists. Configure area routes in *Program.cs* using `MapControllerRoute` with an area pattern, for example: |
233 | 238 | > |
234 | | -> [!code-csharp[](samples/TagHelpersBuiltIn/Startup.cs?name=snippet_UseMvc&highlight=5)] |
| 239 | +> ```csharp |
| 240 | +> app.MapControllerRoute( |
| 241 | +> name: "mvcAreaRoute", |
| 242 | +> pattern: "{area:exists}/{controller=Home}/{action=Index}"); |
| 243 | +> ``` |
235 | 244 |
|
236 | 245 | ### asp-protocol |
237 | 246 |
|
|
0 commit comments