User Tools

Site Tools


guides:tutorials:aspnet-core

Hello Asp.Net Core

Documentation | Tutorials | Hello Asp.Net Core

This tutorial demonstrates how to publish a simple web application showing a map using TatukGIS Asp.Net Core components.

  • Create a default Asp.Net Core application using your favorite dev tool.
  • Open Manage Nuget Packages window and select TatukGIS package source. In the Browse tab choose TatukGIS.DK.ASPNETCore package, install, and accept changes.
  • Optionally, for session support, install System.Runtime.Caching package.
  • Open Program.cs file (VS 2022) or Startup.cs (VS 2019) to configure services:
using TatukGIS.NDK.ASPNET.Extensions; 
...  
builder.Services.AddGISService(-1); // no timeout
builder.Services.AddControllers(); 
... 
app.UseGISHelper(); 
app.MapControllers(); 

Optionally, to control the lifetime of GIS components, a session service can be used along with defined timeout:

builder.Services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => false;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});
 
var timeout = 5;
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromMinutes(timeout);
    options.Cookie.IsEssential = true;
}
);
builder.Services.AddGISService(timeout * 60);

For more options, please check the Samples on how to use session-controlled configuration including timeouts, heartbeat technique, and disposing of components.

  • Open Pages\_ViewImports.cshtml file and register tag helper:
@addTagHelper *, TatukGIS_DK11_ASPNET 
  • Open Pages\Shared\_Layout.cshtml and add code to the head element:
@{ var jspath = Context.Request.PathBase; } 
<script src="@jspath/TatukGIS.NDK.ASPNET/DK_AspNet.min.js"></script> 
<script src="@jspath/TatukGIS.NDK.ASPNET/DK_WebAPI.min.js"></script> 
  • Open Pages\Index.cshtml and paste code :
<div id="GIS_DIV"> 
    <gis-viewer-web id="@ViewData["GIS"]" height="480px" width="640px" image-format="Png"> 
    </gis-viewer-web> 
</div> 

If the tag helper is correct, the gis-viewer-web element should be highlighted in bold.

  • Open Pages\Index.cshtml.cs and paste code :
using TatukGIS.NDK.ASPNET.Services; 
using TatukGIS.NDK.ASPNET.Components; 
 
public class IndexModel : PageModel 
{ 
  private readonly IGIS_Service _gisService; 
 
  public IndexModel(IGIS_Service gisService) 
  { 
    _gisService = gisService; 
  } 
 
  public void OnGet() 
  { 
    TGIS_ViewerWeb GIS = _gisService.ViewerControl("GIS"); 
    ViewData["GIS"] = GIS.Id;
    GIS.Clear(); 
    GIS.Load += GIS_Load; 
  } 
 
  private static void GIS_Load(object sender, EventArgs e) 
  { 
    TGIS_ViewerWeb GIS = (TGIS_ViewerWeb)sender; 
    GIS.Open(GIS.MapContentRootPath(@"\App_Data\wisconsin\LocateCounty.ttkgp")); 
  } 
} 
  • Copy the App_Data directory from Samples to your project's main directory.
  • Run the application. You should see a green map, otherwise check the browser console for errors.
  • In case of the exception “TatukGIS Developer Kernel is not activated”, ensure you have installed and activated the TatukGIS Developer Kernel for ASP.NET product.

Using Asp.Net Core API without viewer

By default, using the DK API requires a viewer component to be initialized on an Html page in the current HttpContext. This happens automatically when using a gis-viewer-web tag helper. If a user doesn't use the viewer and tries to create any class object from the DK API, the exception is raised: You have no license to use TatukGIS Developer Kernel. NOT AN ASP.NET APPLICATION (0x0200).

To activate the license and initialize the viewer instance from the code, the user must :

  • inject IGIS_Service interface into a controller constructor or action
  • initialize a viewer control and set up its properties (like a tag helper does)

This is an example of a default home controller:

using Microsoft.AspNetCore.Mvc;
using TatukGIS.NDK.ASPNET.Components;
using TatukGIS.NDK.ASPNET.Services;
using TatukGIS.NDK.Common;
 
namespace TestDK.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IGIS_Service _gisService;
 
        public HomeController(ILogger<HomeController> logger, IGIS_Service iGIS_Service)
        {
            _logger = logger;
            _gisService = iGIS_Service;
        }
 
        public IActionResult Index()
        {
            var GIS = _gisService.ViewerControl("GIS");
            GIS.WebRootPath = _gisService.WebRootPath;
            GIS.Initialize(new HttpContextEventArgs(HttpContext));
            // now we have access to the viewer and other API
            var gis_internal = GIS.Viewer;
            var gis = new TGIS_ViewerBmp();
            ...
            return View();
        }
    }
}

Initializing DbProviderFactories

.NET Core, unlike .NET Framework, requires registering a database provider before using it for SQL connections. To work with the DK sql layers, the user must :

  • install a package with db client (e.g. System.Data.SqlClient, System.Data.SQLite)
  • register the client instance in DbProviderFactories
DbProviderFactories.RegisterFactory("System.Data.SqlClient", SqlClientFactory.Instance);
DbProviderFactories.RegisterFactory("System.Data.SQLite", System.Data.SQLite.SQLiteFactory.Instance);

Support for MacOS

Since .NET 6 System.Drawing.Common is only supported on Windows. On non-Windows operating systems a TypeInitializationException exception is thrown with PlatformNotSupportedException as the inner exception:

An unhandled exception occurred while processing the request.
PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms.
TypeInitializationException: The type initializer for 'Gdip' threw an exception.

Prior to .NET 6, using the System.Drawing.Common package did not produce any compile-time warnings, and no run-time exceptions were thrown.

To solve this problem on MacOS/linux platforms follow this tutorial .NET Core, Linux and MacOS

2022/10/11 19:57

Page Tools