Skip to content

Example application in ASP.NET Core 6 that use a REST API from Kartverket (The Norwegian Mapping Authority) to search for addresses in Norway.

License

Notifications You must be signed in to change notification settings

royviggo/AddressSearch

Repository files navigation

Address Search in Norway

Codacy Badge .NET

Address Search

Example application in ASP.NET Core 5 that use a REST API from Kartverket (The Norwegian Mapping Authority) to search for addresses in Norway.

This solution have 3 projects:

  • GeonorgeAddressSearch - a Http service, helper methods to set up parameters and all the models used to call and get the results from the REST API
  • AddressSearch.Web - ASP.NET Core 5 MVC application to do a search and show the results with paging
  • AddressSearch.Cmd - a simple console application to do a search with hard coded search terms

How to use GeonorgeAddressSearch in an application

Add a reference to the GeonorgeAddressSearch project or GeonorgeAddressSearch.DLL.

Setup for ASP.NET Core

In Startup.cs call the service extension method:

using GeonorgeAddressSearch;

public void ConfigureServices(IServiceCollection services)
{
    services.AddGeonorgeAddressSearch();
    //...
}

You can set options in the call to AddGeonorgeAddressSearch, but the only option is BaseUrl, and the default value https://ws.geonorge.no/adresser/v1 is already set in the service, so you probably don't need to change that:

services.AddGeonorgeAddressSearch(options =>
{
    options.BaseUrl = "https://ws.geonorge.no/adresser/v1";
});

Another way is to send a GeonorgeAddressSearchOptions object as a parameter. This example show how to get it from Configuration:

var geonorgeAddressSearchOptions = Configuration
    .GetSection(GeonorgeAddressSearchOptions.Section)
    .Get<GeonorgeAddressSearchOptions>();

services.AddGeonorgeAddressSearch(geonorgeAddressSearchOptions);

Injecting the service into a controller

To inject the service in a controller:

using GeonorgeAddressSearch;

private readonly IGeonorgeAddressSearchService _searchService;

public MyController(IGeonorgeAddressSearchService searchService)
{
    _searchService = searchService;
}

Setup for a console application

In a console application you need to set up dependency injection, call the service extension method and get the service from the interface:

using GeonorgeAddressSearch;
using Microsoft.Extensions.DependencyInjection;

var serviceProvider = new ServiceCollection()
    .AddGeonorgeAddressSearch()
    .BuildServiceProvider();

var _searchService = serviceProvider.GetService<IGeonorgeAddressSearchService>();

Searching for addresses

Create an AddressSearchRequest object and assign the attributes to search for. The Sok attribute is used to search in many fields and you can use wildcards (*) to search for part of a word.

var searchRequest = new AddressSearchRequest
{
    Sok = "storgat* mo*",
    Sokemodus = SearchMode.AND,
};
var searchResult = await _searchService.SearchAsync(searchRequest);

About

Example application in ASP.NET Core 6 that use a REST API from Kartverket (The Norwegian Mapping Authority) to search for addresses in Norway.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published