AssetModel
AssetModel
AssetModel holds the file and the details of an asset to upload. Pass it to the Create method to upload a new asset, or to the Update method to replace the file of an existing asset.
When to Use This Class: Use this class every time your code uploads a file to a stack, from a file path, a stream, or bytes in memory.
Conceptual Role: Acts as the request body of an upload. The SDK turns it into a multipart/form-data form through the IUploadInterface interface that AssetModel implements.
Returns: AssetModel. The file and details of an asset upload.
Constructors
AssetModel has four constructors. They differ only in the source of the file content. Each one takes the same optional details after contentType.
From a file path: AssetModel(string fileName, string filePath, string contentType, string? title = null, string? description = null, string? parentUID = null, string? tags = null)
From a stream: AssetModel(string fileName, Stream stream, string contentType, ...)
From bytes: AssetModel(string fileName, byte[] bytes, string contentType, ...)
From HTTP content: AssetModel(string fileName, ByteArrayContent byteArray, string contentType, ...)
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| fileName | string | Yes | Not applicable | Names the file in the upload form. |
| filePath | string | Yes (file path form) | Not applicable | Reads the file at this path. |
| stream | Stream | Yes (stream form) | Not applicable | Reads the file from this stream. |
| bytes | byte[] | Yes (bytes form) | Not applicable | Uses these bytes as the file. |
| byteArray | ByteArrayContent | Yes (HTTP content form) | Not applicable | Uses this content as the file. |
| contentType | string | Yes | Not applicable | Sets the media type of the file, for example image/png. |
| title | string? | No | null | Sets the asset title. |
| description | string? | No | null | Sets the asset description. |
| parentUID | string? | No | null | Places the asset in this folder. |
| tags | string? | No | null | Sets the asset tags. |
Properties
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| FileName | string | Yes | The fileName constructor argument | File name in the upload form. Sent as the file name of asset[upload]. |
| ContentType | string | Yes | The contentType constructor argument | Media type of the file. |
| Title | string? | No | The title constructor argument | Asset title. Sent as asset[title]. |
| Description | string? | No | The description constructor argument | Asset description. Sent as asset[description]. |
| ParentUID | string? | No | The parentUID constructor argument | UID of the folder that holds the asset. Sent as asset[parent_uid]. |
| Tags | string? | No | The tags constructor argument | Asset tags as one string. Sent as asset[tags] without changes. |
Class-Level Notes
The constructor reads the file.
- The file path form opens the file and reads it into memory when you create the model, not when the SDK sends the request. A later change to the file does not reach the upload.
- The stream form reads stream.Length bytes from the current position of the stream. Set Position to 0 first if you read from the stream before.
- The bytes form and the HTTP content form keep the content you pass.
Constructor checks.
- Throws ArgumentNullException if fileName is null. An empty fileName passes the check.
- Throws ArgumentNullException if bytes or byteArray is null. A null stream throws NullReferenceException.
- .NET validates contentType when the SDK sets it on the file content. A null value throws ArgumentNullException, an empty value throws ArgumentException, and a value that is not a media type throws FormatException.
- The file path form throws the .NET file exceptions, for example FileNotFoundException or DirectoryNotFoundException, when .NET cannot open the file.
- The stream form throws EndOfStreamException when the stream holds fewer bytes after its current position than stream.Length, and NotSupportedException when the stream does not report a length.
Form fields.
- The SDK sends asset[upload] with every request, and each of asset[title], asset[description], asset[parent_uid], and asset[tags] only when its property is not null.
- The Content Management API (CMA) Upload asset request documents these form fields.
Method Index
| Method Name | Returns | Description |
|---|---|---|
| GetHttpContent | The multipart form body of the upload. | Builds the form that the SDK sends. |
Class-Level Snippet
The snippet below builds a model from a file path with every detail set, and uploads it into a folder.
using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
AssetModel model = new AssetModel(
"banner.png",
"<FILE_PATH>",
"image/png",
title: "Home Banner",
description: "Hero image for the home page",
parentUID: "<FOLDER_UID>",
tags: "banner");
try
{
ContentstackResponse response = client.Stack("<API_KEY>").Asset().Create(model);
Console.WriteLine(response.OpenJsonObjectResponse()["asset"]?["uid"]);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}Methods
Each method of the AssetModel class has its own section below, with its parameters, validation rules, behavior, and examples.
GetHttpContent
Returns: HttpContent. The multipart form body of the upload.
Validation
- Throws no exception of its own. The constructor already checked the file name and the file content.
- GetHttpContent sends no request, so it raises no API error.
Behavior
- Returns a new MultipartFormDataContent on each call.
- Adds the file under asset[upload] with FileName as its file name.
- Adds asset[title], asset[description], asset[parent_uid], and asset[tags] only for the properties that are not null.
- Reads the properties when you call it, so a property you change after construction appears in the form. The file content stays the content the constructor received.
Example
Basic usage: inspect the form the SDK uploads
using System;
using System.Net.Http;
using System.Text;
using Contentstack.Management.Core.Models;
byte[] bytes = Encoding.UTF8.GetBytes("{\"campaign\": \"spring\"}");
AssetModel model = new AssetModel("campaign.json", bytes, "application/json", title: "Campaign Settings");
HttpContent content = model.GetHttpContent();
Console.WriteLine(content.Headers.ContentType);
Console.WriteLine(await content.ReadAsStringAsync());