Get Started with .NET SDK and Live Preview
This guide will help you get started with Contentstack .NET SDK to build apps powered by Contentstack.
Prerequisites
- .NET version 2.0 or later
- Live Preview Utils SDK
SDK Installation and Setup
To install the .NET SDK, choose either of the following methods:
Via Package Manager: Open the terminal and install the contentstack module using the following “Package Manager” command:
PM> Install-Package contentstack.csharp
Via .NET CLI: Run the following .NET CLI command:
dotnet add package contentstack.csharp
After successful installation, to use the module in your application, you need to first add a namespace to your class:
using Contentstack.Core; // ContentstackClient
using Contentstack.Core.Models; // Stack, Query, Entry, Asset, ContentType
using Contentstack.Core.Configuration; // ContentstackOptions
Initializing the Stack with Live Preview
Since the Live Preview Utils SDK is responsible for communication, you need to initialize it within your stack.
Add the appsettings.json command to initialize the stack:
{
"ContentstackOptions": {
"ApiKey": "<api_key>",
"DeliveryToken": "<delivery_token>",
"Environment": "<environment>",
"LivePreview": {
"PreviewToken" : "<preview_token>",
"Enable" : true,
"Host" : "rest-preview.contentstack.com"
}
}
}
Note: By default, the Host parameter points to the North America endpoint. If your website is hosted on the European data center, then pass the European endpoint against the Host parameter.
Configure the HTTP Request Pipeline
Use the following method to configure the HTTP request pipeline:
// Startup.cs
...
//Add following
using Contentstack.Core;
using System.Linq;
using Microsoft.Extensions.Primitives;
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
services.AddContentstack(Configuration); //Contentstack Configuration
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()); //CORS to allow Contentstack origin
app.Use(async (context, next) =>
{
var contentstackClient = app.ApplicationServices.GetService<ContentstackClient>();
var dict = context.Request.Query.Keys.Cast<string>().ToDictionary(k => k, v =>
{
StringValues hash;
context.Request.Query.TryGetValue(v, out hash);
return hash.ToString();
});
try
{
await contentstackClient.LivePreviewQueryAsync(dict);
}
catch { }
await next.Invoke();
});
}
}
For Server-side Rendered Websites
To install and initialize the Live Preview Utils SDK, you can refer to our SSR Live Preview Setup documentation.
Add the following script within the head tag of the “_Host.chtml” or “Index.chtml” file:
...
ContentstackLivePreview.init({
enable: true,
ssr: true,
stackDetails: {
apiKey: "API_KEY", //Stack API key
},
});
...
Query Request
Contentstack SDKs let you interact with the Content Delivery APIs and retrieve content from Contentstack. They are read-only in nature. The SDKs fetch and deliver content from the nearest server via Fastly, our powerful and robust CDN.
To get an entry, you need to specify the content type UID and the UID of the entry.
client.ContentType("CONTENT_TYPE_UID").Query().Find();
client.ContentType("CONTENT_TYPE_UID").Entry(“ENTRY_UID”).Find();