{"id":8360,"date":"2020-07-08T22:31:03","date_gmt":"2020-07-08T22:31:03","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8360"},"modified":"2020-07-08T22:31:03","modified_gmt":"2020-07-08T22:31:03","slug":"blazor-routing-and-navigation","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/blazor-routing-and-navigation\/","title":{"rendered":"Blazor routing and Navigation"},"content":{"rendered":"<p><strong>Routing<\/strong><\/p>\n<p>If you take a look at the generated pages for a Blazor application, you&#8217;ll see the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@page &quot;\/counter&quot;\r\n<\/pre>\n<p>this denotes a route to our page, i.e. http:\/\/localhost:44319\/counter. <\/p>\n<p><em>Note: When you compile your application a file is generated (see Counter.g.cs in compiled folder) and this page declaration is converted to a RouteAttribute.<\/em><\/p>\n<p>You can have multiple routes pointing to the same page, in this example all these routes will navigate to the same Counter.razor page <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@page &quot;\/counter&quot;\r\n@page &quot;\/mycounter&quot;\r\n@page &quot;\/something&quot;\r\n<\/pre>\n<p>Probably more useful is the ability to add parameters to our routes, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@page &quot;\/counter&quot;\r\n@page &quot;\/counter\/{InitialValue}&quot;\r\n<\/pre>\n<p>By default InitialValue is of type string, but we can change this by using a constraint, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@page &quot;\/counter&quot;\r\n@page &quot;\/counter\/{InitialValue:int}&quot;\r\n<\/pre>\n<p>Now we simply create a property in our code with the ParameterAttribute applied to it, like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;Parameter]\r\npublic int? InitialValue { get; set; }\r\n<\/pre>\n<p>We use a nullable int in case the value is missing. We can also have routes handling different type contraints (for example a string or on int) to mean two different things<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@page &quot;\/counter&quot;\r\n@page &quot;\/counter\/{InitialValue:int}&quot;\r\n@page &quot;\/counter\/{SomeString}&quot;\r\n<\/pre>\n<p>In our OnInitialized() method (part of the component life cycle) we might then set the currentCount to InitialValue.<\/p>\n<p>The routes are declared in our pages but the routing component itself can be found in App.razor. In this case the Router will locate the routes from the supplied AppAssembly<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;Router AppAssembly=&quot;@typeof(Program).Assembly&quot;&gt;\r\n<\/pre>\n<p>We can also includes routes within AdditionalAssemblies, for example maybe you&#8217;ve got some reusable components with routing supplied, hence we write<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;Router AppAssembly=&quot;@typeof(Program).Assembly&quot; \r\n   AdditionalAssemblies=&quot;new&#x5B;] { typeof(MyComponents).Assembly }&gt;\r\n<\/pre>\n<p><strong>Navigation<\/strong><\/p>\n<p>If you want to navigate to other pages, we need to use the <em>NavigationManager<\/em>. This needs to be injected into our page, using<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inject NavigationManager NavManager\r\n<\/pre>\n<p>Now to navigate, for example when a button is click, we use the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nNavManager.NavigateTo(&quot;counter\/2&quot;);\r\n<\/pre>\n<p>In the instance we&#8217;ll navigate to the relative URL\/page with &#8220;counter\/someData&#8221;.<\/p>\n<p>This works fine when navigating to other pages but if we want to navigate within a page and we&#8217;re using the component&#8217;s OnInitialized method to initialize data? <\/p>\n<p>Because OnInitialized is called when the page is initial loaded and because we&#8217;re already in the page, this method will not normally be called again if we navigate within the page, so we simply need to supply an extra parameter to our NavigateTo code to force the page to reload, i.e. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nNavManager.NavigateTo(&quot;counter\/2&quot;, true);\r\n<\/pre>\n<p><em>Note: An alternative solution ofcourse is to override the other life cycle method OnParameterSet and instead move initialization code to that method.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Routing If you take a look at the generated pages for a Blazor application, you&#8217;ll see the following @page &quot;\/counter&quot; this denotes a route to our page, i.e. http:\/\/localhost:44319\/counter. Note: When you compile your application a file is generated (see Counter.g.cs in compiled folder) and this page declaration is converted to a RouteAttribute. You can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305],"tags":[],"class_list":["post-8360","post","type-post","status-publish","format-standard","hentry","category-blazor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8360"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8360\/revisions"}],"predecessor-version":[{"id":8380,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8360\/revisions\/8380"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}