Getting Started
Below instructions are for creating a Blazor application from scratch and adding GeoBlazor. Adding GeoBlazor to an existing application would be the same, beginning with step 2.
Alternatively, you can use the new GeoBlazor Templates to create a new Blazor application with GeoBlazor already installed.
- Create a new Blazor Web App (.NET 8), Blazor Server, Blazor Wasm, or Blazor Hybrid (MAUI) project, using the templates provided in your IDE or the
dotnet
CLI. If creating a Blazor Web App, be sure to also select anInteractive Render Mode
, either Server, WebAssembly, or Auto. - Add a
PackageReference
to the latest version of thedymaptic.GeoBlazor.Core
ordymaptic.GeoBlazor.Pro
(pro is a paid version with more features) package via your IDE’s Nuget Package Manager or from the command line withdotnet add package dymaptic.GeoBlazor.Core
(ordotnet add package dymaptic.GeoBlazor.Pro
). For Blazor Web Apps supporting WebAssembly, add this reference to the.Client
WebAssembly project, or a Razor Class Library where you intend to write your mapping Razor components. -
The ArcGIS API requires some form of authentication. The simplest is to use an API Key. Generate a key from the ArcGIS Developer Dashboard. For Blazor Server, place it in your appsettings.json, like this:
{ "Logging": ..., "AllowedHosts": ..., "ArcGISApiKey": "yourKeyValue" }
Note: If you are using Blazor WASM, there are several issues with this approach. First,appsettings.json
is not added by default to all templates. If you want to add it yourself, you need to add it inside thewwwroot
folder. For Blazor Web Apps with WebAssembly, you must define the API key in _both_ projects. Be Aware that the API key will be exposed in the browser (just like it would with Javascript). Even when using Blazor Server, the API key may be present in HTTP requests visible to the user in the browsers dev tools, so you should probably take other steps like setting up referrer rules in ArcGIS.You can also set up an OAuth2 workflow, which is more secure as it does not expose a static API key, but this is more complex. You can read about all the authentication options in Authentication. -
In the root file that defines your html, add the following lines to the
<head>
section. This would be_Layout.cshtml
for Blazor Server,index.html
for Blazor Wasm and Blazor Hybrid, orApp.razor
for Blazor Web Apps. Note thatYourProject
is the namespace for the application that you are creating.<link href="_content/dymaptic.GeoBlazor.Core"/> <link href="_content/dymaptic.GeoBlazor.Core/assets/esri/themes/light/main.css" rel="stylesheet" /> <link href="YourProject.styles.css" rel="stylesheet" />
or (dark theme)
<link href="_content/dymaptic.GeoBlazor.Core"/> <link href="_content/dymaptic.GeoBlazor.Core/assets/esri/themes/dark/main.css" rel="stylesheet" /> <link href="YourProject.styles.css" rel="stylesheet" />
Note: You may already have the `YourProject.styles.css` file. If so, you can just add the two lines to the existing file. In some .Net templates, this file is commented out by default and you will need to add it. -
In
_Imports.razor
(for each executable project), add the following lines up front, or you can add as needed to resolve code that you consume.@using dymaptic.GeoBlazor.Core.Components @using dymaptic.GeoBlazor.Core.Components.Geometries @using dymaptic.GeoBlazor.Core.Components.Layers @using dymaptic.GeoBlazor.Core.Components.Popups @using dymaptic.GeoBlazor.Core.Components.Renderers @using dymaptic.GeoBlazor.Core.Components.Renderers.ColorRamps @using dymaptic.GeoBlazor.Core.Components.Symbols @using dymaptic.GeoBlazor.Core.Components.Views @using dymaptic.GeoBlazor.Core.Components.Widgets @using dymaptic.GeoBlazor.Core.Events @using dymaptic.GeoBlazor.Core.Model @using dymaptic.GeoBlazor.Core.Objects
-
In
Program.cs
(for each executable project), add the following line to yourbuilder.Services
to inject logic components likeGeometryEngine
.builder.Services.AddGeoBlazor(builder.Configuration);
If you are using Blazor Server or InteractiveServer mode in Blazor Web Apps, you should also add the following lines to
Program.cs
to support the.wsv
file type.var provider = new FileExtensionContentTypeProvider(); provider.Mappings[".wsv"] = "application/octet-stream"; app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });
If you wish to exclude JavaScript
.map
files (useful for debugging, but are a large download for end users), update theprovider
code above to exclude.map
files.var provider = new FileExtensionContentTypeProvider(); provider.Mappings[".wsv"] = "application/octet-stream"; #if RELEASE provider.Mappings.Remove(".map"); #endif app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });
-
Create a new Razor Component in the
Pages
folder, or just useIndex.razor
. Add aMapView
. You must provide eitherStyle
orClass
attributes that give the view aheight
andwidth
in order to render properly.@page "/" <MapView Longitude="_longitude" Latitude="_latitude" Zoom="11" Style="height: 400px; width: 100%;"> </MapView> @code { private readonly double _latitude = 34.027; private readonly double _longitude = -118.805; }
-
Within the
MapView
, define a map using theWebMap
component. To load a pre-generated map from ArcGIS Online or Portal, get the Map Id (PortalItem Id) of the map.<MapView Longitude="_longitude" Latitude="_latitude" Zoom="11" Style="height: 400px; width: 100%;"> <WebMap> <PortalItem Id="4a6cb60ebbe3483a805999d481c2daa5" /> </WebMap> </MapView>
-
Add a Widget to the
MapView
, after theWebMap
.<MapView Longitude="_longitude" Latitude="_latitude" Zoom="11" Style="height: 400px; width: 100%;"> <WebMap> <PortalItem Id="4a6cb60ebbe3483a805999d481c2daa5" /> </WebMap> <ScaleBarWidget Position="OverlayPosition.BottomLeft" /> </MapView>
- Run your application and make sure you can see your map!
- Now that you have a great starting point, you can now start to customize the features available in your new app using Geoblazor’s capabilites:
-Take a look at the Documentation pages to learn more. - Optional: To remove the
Thank You
message from your console messages, please consider registering your GeoBlazor Core application on the dymaptic licensing server. After registering and filling out a small questionnaire, you will receive a registration key, which you can add to any configuration (e.g.,appsettings.json
) to remove the message.
"Logging": ...,
"AllowedHosts": ...,
"ArcGISApiKey": "YOUR_ARCGIS_API_KEY",
"GeoBlazor": {
"RegistrationKey": "YOUR_REGISTRATION_KEY"
}
Also, don’t forget to check out GeoBlazor Pro for more features and support!