Fragment



The asp-fragment attribute defines a URL fragment to append to the URL. The Anchor Tag Helper adds the hash character (#). Consider the following markup: asp-controller='Speaker' asp-action='Evaluations' asp-fragment='SpeakerEvaluations'Speaker Evaluations The generated HTML. A fragment is a part of a sentence, but can’t be a sentence on its own. A fragment is a part of a sentence, but can’t be a sentence on its own. If you're seeing this message, it means we're having trouble loading external resources on our website. Definition In English grammar, a sentence fragment is a group of words that begins with a capital letter and ends with a period, question mark, or exclamation point but is grammatically incomplete.

-->

By Peter Kellner and Scott Addie

The Anchor Tag Helper enhances the standard HTML anchor (<a ... ></a>) tag by adding new attributes. By convention, the attribute names are prefixed with asp-. The rendered anchor element's href attribute value is determined by the values of the asp- attributes.

For an overview of Tag Helpers, see Tag Helpers in ASP.NET Core.

View or download sample code (how to download)

SpeakerController is used in samples throughout this document:

Anchor Tag Helper attributes

asp-controller

The asp-controller attribute assigns the controller used for generating the URL. The following markup lists all speakers:

The generated HTML:

If the asp-controller attribute is specified and asp-action isn't, the default asp-action value is the controller action associated with the currently executing view. If asp-action is omitted from the preceding markup, and the Anchor Tag Helper is used in HomeController's Index view (/Home), the generated HTML is:

asp-action

The asp-action attribute value represents the controller action name included in the generated href attribute. The following markup sets the generated href attribute value to the speaker evaluations page:

The generated HTML:

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

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

asp-route-{value}

The asp-route-{value} attribute enables a wildcard route prefix. Any value occupying the {value} placeholder is interpreted as a potential route parameter. If a default route isn't found, this route prefix is appended to the generated href attribute as a request parameter and value. Otherwise, it's substituted in the route template.

Consider the following controller action:

With a default route template defined in Startup.Configure:

The MVC view uses the model, provided by the action, as follows:

The default route's {id?} placeholder was matched. The generated HTML:

Assume the route prefix isn't part of the matching routing template, as with the following MVC view:

The following HTML is generated because speakerid wasn't found in the matching route:

If either asp-controller or asp-action aren't specified, then the same default processing is followed as is in the asp-route attribute.

asp-route

The asp-route attribute is used for creating a URL linking directly to a named route. Using routing attributes, a route can be named as shown in the SpeakerController and used in its Evaluations action:

In the following markup, the asp-route attribute references the named route:

The Anchor Tag Helper generates a route directly to that controller action using the URL /Speaker/Evaluations. The generated HTML:

If asp-controller or asp-action is specified in addition to asp-route, the route generated may not be what you expect. To avoid a route conflict, asp-route shouldn't be used with the asp-controller and asp-action attributes.

asp-all-route-data

The asp-all-route-data attribute supports the creation of a dictionary of key-value pairs. The key is the parameter name, and the value is the parameter value.

In the following example, a dictionary is initialized and passed to a Razor view. Alternatively, the data could be passed in with your model.

The preceding code generates the following HTML:

The asp-all-route-data dictionary is flattened to produce a querystring meeting the requirements of the overloaded Evaluations action:

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

asp-fragment

The asp-fragment attribute defines a URL fragment to append to the URL. The Anchor Tag Helper adds the hash character (#). Consider the following markup:

The generated HTML:

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

Fragmentation

asp-area

The asp-area attribute sets the area name used to set the appropriate route. The following examples depict how the asp-area attribute causes a remapping of routes.

Usage in Razor Pages

Razor Pages areas are supported in ASP.NET Core 2.1 or later.

Consider the following directory hierarchy:

  • {Project name}
    • wwwroot
    • Areas
      • Sessions
        • Pages
          • _ViewStart.cshtml
          • Index.cshtml
          • Index.cshtml.cs
    • Pages

The markup to reference the Sessions area Index Razor Page is:

The generated HTML:

Tip

To support areas in a Razor Pages app, do one of the following in Startup.ConfigureServices:

  • Set the compatibility version to 2.1 or later.

  • Set the RazorPagesOptions.AllowAreas property to true:

Usage in MVC

Consider the following directory hierarchy:

  • {Project name}
    • wwwroot
    • Areas
      • Blogs
        • Controllers
          • HomeController.cs
        • Views
          • Home
            • AboutBlog.cshtml
            • Index.cshtml
          • _ViewStart.cshtml
    • Controllers

Setting asp-area to 'Blogs' prefixes the directory Areas/Blogs to the routes of the associated controllers and views for this anchor tag. The markup to reference the AboutBlog view is:

The generated HTML:

Tip

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:

asp-protocol

The asp-protocol attribute is for specifying a protocol (such as https) in your URL. For example:

The generated HTML:

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

asp-host

The asp-host attribute is for specifying a host name in your URL. For example:

Fragmented

The generated HTML:

asp-page

The asp-page attribute is used with Razor Pages. Use it to set an anchor tag's href attribute value to a specific page. Prefixing the page name with a forward slash ('/') creates the URL.

The following sample points to the attendee Razor Page:

The generated HTML:

The asp-page attribute is mutually exclusive with the asp-route, asp-controller, and asp-action attributes. However, asp-page can be used with asp-route-{value} to control routing, as the following markup demonstrates:

The generated HTML:

asp-page-handler

The asp-page-handler attribute is used with Razor Pages. It's intended for linking to specific page handlers.

Consider the following page handler:

The page model's associated markup links to the OnGetProfile page handler. Note the On<Verb> prefix of the page handler method name is omitted in the asp-page-handler attribute value. When the method is asynchronous, the Async suffix is omitted, too.

The generated HTML:

Additional resources

(Redirected from Fragments)
Look up Fragment, Fragments, fragment, fragmental, or fragments in Wiktionary, the free dictionary.
Wikimedia Commons has media related to Fragments.

Fragment may refer to:

Entertainment[edit]

Television and film[edit]

  • 'Fragments' (Torchwood), an episode from the BBC TV series
  • 'Fragments', an episode from the Canadian TV series Sanctuary
  • 'Fragments', an episode from the American TV series Steven Universe Future
  • Fragments (film) (a.k.a. Winged Creatures), a 2009 film
  • Fragments: Chronicle of a Vanishing, a 1991 Croatian film

Music[edit]

  • 'Fragments', a song from Endless Wire (The Who album)
  • Fragments (album), a 1987 album by jazz pianist Paul Bley
  • Fragments, an album by the Danish singer Jakob Sveistrup
  • Fragments (EP), an EP by Rapids!
  • Fragments, an EP by Chipzel

Other[edit]

  • Fragments, a play by Edward Albee
  • Fragments: Memories of a Wartime Childhood 1939–1948, a fictional memoir of Holocaust survival by Binjamin Wilkomirski
  • .hack//fragment, an online and offline RPG from the .hack video game series
  • Fragment (novel), a novel by Warren Fahy

Other[edit]

  • Fragment (computer graphics), all the data necessary to generate a pixel in the frame buffer
  • Fragment (logic), a syntactically restricted subset of a logical language
  • URI fragment, the component of a URL following the '#' that identifies a portion of a larger document
  • Literary fragment, a brief or unfinished work of prose
  • Manuscript fragment, a remnant of a handwritten book
  • Sentence fragment, a sentence not containing a subject or a predicate

See also[edit]

Fragments

Retrieved from 'https://en.wikipedia.org/w/index.php?title=Fragment&oldid=1018739648'