Posts

Showing posts from December, 2021

Component Disposal in Blazor

Image
Developers need to manage the resources used in the application. They need to release unmanaged resources that will help prevent memory leaks. The Blazor provides two implementations for releasing the component. IDisposable and IAsyncDisposable IDisposable The following is a small example of the code to illustrate the disposal of components in Blazor. The first step is to implement the IDisposable interface using the @implements directive. The Dispose() method is then used to release the memory. @page "/sample" @implements IDisposable @code { Movie movieObj; private void CollectValues() { movieObj = new Movie(); ... ... ... } public void Dispose() { movieObj?.Dispose(); } } If only one object requires disposal, you can use the lambda expression. public void Dispose() => movieObj?.Dispose(); IAsyncDisposable You can use IAsyncDisposable.DisposeAsync for asynchronous disposal. The follo

Query String in Blazor

Image
This blog is going to explain how to send values from one component to another using the query string in Blazor. The query string is the value appended with the URL. Below is an example of a query string. https://localhost:44319/result ?firstname=Peter&lastname=Parker The value pair that comes after the question mark (?) is a query string. Key and value are divided by equal (=). Multiple query strings are separated by ampersand(&). You have to use the Microsoft.AspNetCore.WebUtilities package to implement the query string in Blazor. This package can be installed from the nuget package. This package helps to manage the query string. Install-Package Microsoft.AspNetCore.WebUtilities Let's look at a small example. The following is a razor component code. This will get the query string value and display it on the page. First it obtains a complete URI using the ToAbsoluteUri() method. The query string is then extracted using the QueryHelpers.ParseQuery() method.