Page 55 - MSDN Magazine, May 2019
P. 55
values that can’t be converted successfully to the specified type invalidate the match and the route isn’t recognized.
Smarter Links and Programmatic URL Navigation
In a Blazor application, you’re welcome to use anchor tags to cre- ate links to external content. However, when anchor tags are used to render a menu or a navigation bar, some additional work may be necessary to adjust CSS styles and to reflect the state of the link.
The built-in Blazor NavLink component can be used wherever an anchor element is required, but especially in a menu. The difference between a canonical HTML anchor element and the NavLink com- ponent is in the automatic assignment of the “active” style when the current address matches the link. The “active” CSS class is automati- cally added to the anchor tag rendered by the NavLink component if the current page URL matches the referenced URL. The implemen- tation of the “active” CSS class remains the responsibility of the page developer. The component also includes a property that controls how matching is done. You can do a strict match or a prefix match.
The Blazor router can also be triggered programmatically. To navigate via code from within a Blazor page, you should first inject the configured dependency for the IUriHelper abstract type. The @inject directive does the job, as shown here:
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper Navigator
The injected object can be commanded via the method Navigate- To. The method takes the URL as an argument:
Navigator.NavigateTo(“/user/view/1”);
The method is conceptually equivalent to setting the href prop- erty of the DOM location object in pure JavaScript. In Blazor, though, the router makes the magic of navigating without leaving the client and without fully reloading the content from the server.
What’s Missing
The Blazor framework is an attractive piece of software, but one that remains very much in the works. There are a number of miss- ing routing features—for example, the ability to attach roles or user identities to a route—and authentication and authorization is still incomplete. Any consideration around security-related facilities in routes must wait until those APIs are finalized.
Another important piece of the routing puzzle that’s missing: The ability to fully customize the logic of the router that decides about the target URL. This feature would help developers gain control over invalid link requests. While the Blazor router is far from finished, work contin- ues toward a mature, shipping framework. You can view enhancements to the Blazor routing system tracked by the team at bit.ly/2TtY0DP. n
Dino Esposito has authored more than 20 books and 1,000-plus articles in his 25-year career. Author of “The Sabbatical Break,” a theatrical-style show, Esposito is busy writing software for a greener world as the digital strategist at BaxEnergy. Follow him on Twitter: @despos.
thanks to the following Microsoft technical expert for reviewing this article: Daniel Roth
Untitled-2 1
1/8/19 10:52 AM