Skip to content

Documents

Joel Mitchell edited this page Jan 18, 2017 · 5 revisions

Managing Document Assets

Document assets can be any non-media file that you might want to make available for download on your site e.g. pdfs, word docs or zip files.

The admin panel has a whole section devoted to managing documents where users can add, update and delete document assets from a central place.

Generating Document Urls

From a View or Template

The Cofoundry View Helper is the best way to access this:

@inherits CofoundryPageModule<MyContentDisplayModel>
@using Cofoundry.Web

/* From a model */
<img src="@Cofoundry.Routing.DocumentAsset(Model.MyDocumentAsset)">

/* From an id (see warning below) */
<img src="@Cofoundry.Routing.DocumentAsset(3)">

Warning: Generating a url from just a document asset id involves getting more information about the document from the database. It is recommended that you try to include the full DocumentAssetRenderDetails object in your view model to take advantage of batch requests and async methods.

From Code

You can request IDocumentAssetRouteLibrary from the DI container and use this to generate urls. It is the same api used by the Cofoundry View Helper above.

public class DocumentExample
{
    private IDocumentAssetRouteLibrary _documentAssetRouteLibrary;

    public DocumentExample(IDocumentAssetRouteLibrary documentAssetRouteLibrary)
    {
        _documentAssetRouteLibrary = documentAssetRouteLibrary;
    }

    public string GetExampleUrl(IDocumentAssetRenderable document)
    {
        var url = _documentAssetRouteLibrary.DocumentAsset(document);

        return url;
    }
}

Getting Document Data

The simplest way to get document data is by resolving an instance of IDocumentAssetRepository from the DI container.

public class DocumentExample
{
    private IDocumentAssetRouteLibrary _documentAssetRouteLibrary;
    private IDocumentAssetRepository _documentAssetRepository;

    public DocumentExample(
        IDocumentAssetRouteLibrary documentAssetRouteLibrary,
        IDocumentAssetRepository documentAssetRepository
        )
    {
        _documentAssetRouteLibrary = documentAssetRouteLibrary;
        _documentAssetRepository = documentAssetRepository;
    }

    public Task<string> GetExampleUrl(int documentId)
    {
        var document = await _documentAssetRepository.GetDocumentAssetRenderDetailsByIdAsync(documentId);
        var url = _documentAssetRouteLibrary.DocumentAsset(document);

        return url;
    }
}

Alternatively you can resolve an instance of CofoundryDbContext from the DI container and use Entity Framework to completely customize your query.

Clone this wiki locally