---
sidebar_position: 7
title: Redirection
---

## Perform a redirect

Now that you can add an item, it would be interesting to redirect the user to the list when adding it.

Open the `Pages/Add.razor.cs` file and add the highlighted lines:

```csharp title="Pages/Add.razor.cs"
public partial class Add
{
	[Inject]
	public ILocalStorageService LocalStorage { get; set; }

// highlight-start
	[Inject]
	public NavigationManager NavigationManager { get; set; }
// highlight-end

	/// <summary>
	/// The default roles.
	/// </summary>
	private List<string> roles = new List<string>() { "admin", "writter", "reader", "member" };

	/// <summary>
	/// The current data model
	/// </summary>
	private DataModel dataModel = new()
	{
		DateOfBirth = DateTime.Now,
		Roles = new List<string>()
	};
	
	private async void HandleValidSubmit()
	{
		// Get the current data
		var currentData = await LocalStorage.GetItemAsync<List<Data>>("data");

		// Simulate the Id
		dataModel.Id = currentData.Max(s => s.Id) + 1;

		// Add the item to the current data
		currentData.Add(new Data
		{
			LastName = dataModel.LastName,
			DateOfBirth = dataModel.DateOfBirth,
			FirstName = dataModel.FirstName,
			Id = dataModel.Id,
			Roles = dataModel.Roles
		});

		// Save the data
		await LocalStorage.SetItemAsync("data", currentData);

// highlight-next-line
        NavigationManager.NavigateTo("list");
	}
	
	private void OnRoleChange(string item, object checkedValue)
	{
		if ((bool)checkedValue)
		{
			if (!dataModel.Roles.Contains(item))
			{
				dataModel.Roles.Add(item);
			}

			return;
		}
		
		if (dataModel.Roles.Contains(item))
		{
			dataModel.Roles.Remove(item);
		}
	}
}
```

## Concept: URI and navigation state helpers

Use `NavigationManager` to manage URIs and navigation in C# code. `NavigationManager` provides the event and methods shown in the following table.

| Membre | Description |
| ---- | ---- |
| Uri | Gets the current absolute URI. |
| BaseUri | Gets the base URI (with a trailing slash) that can be prepended to relative URI paths to produce an absolute URI. Typically, `BaseUri` corresponds to the `href` attribute on the document's `<base>` element in `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Layout.cshtml` (Blazor Server). |
| NavigateTo | Navigates to the specified URI. If `forceLoad` is `true`: <ul><li>Client-side routing is bypassed.</li><li>The browser is forced to load the new page from the server, whether or not the URI is normally handled by the client-side router.</li></ul>If `replace` is `true`, the current URI in the browser history is replaced instead of pushing a new URI onto the history stack. |
| LocationChanged | An event that fires when the navigation location has changed.  |
| ToAbsoluteUri | Converts a relative URI into an absolute URI. |
| ToBaseRelativePath | 	Given a base URI (for example, a URI previously returned by BaseUri), converts an absolute URI into a URI relative to the base URI prefix. |
| GetUriWithQueryParameter | Returns a URI constructed by updating NavigationManager.Uri with a single parameter added, updated, or removed. |
