Skip to content

Latest commit

 

History

History
90 lines (70 loc) · 3.29 KB

File metadata and controls

90 lines (70 loc) · 3.29 KB

Simplify.Web.Multipart

Nuget Version Nuget Download Build Package Libraries.io dependency status for latest release CodeFactor Grade Platform

Simplify.Web.Multipart is a package which provides multipart form view model and model binder for Simplify.Web web-framework.

Quick start

Registering binder

public void Configuration(IApplicationBuilder app)
{
    // ...existing code...
    HttpModelHandler.RegisterModelBinder<HttpMultipartFormModelBinder>();
    // ...existing code...
    app.UseSimplifyWeb();
}

public void ConfigureServices(IServiceCollection services)
{
    // ...existing code...
    DIContainer.Current.RegisterHttpMultipartFormModelBinder();
    // ...existing code...
}

Getting files from client

Asynchronous

public class MyController : ControllerAsync<MultipartViewModel>
{
    public override async Task<ControllerResponse> Invoke()
    {
        await ReadModelAsync();

        Model.Files;
    }
}

Synchronous

Multipart files will be deserialized to the controller model on first model access

public class MyController : Controller<MultipartViewModel>
{
    public override ControllerResponse Invoke()
    {
        Model.Files;
    }
}

Binding parameters to a strongly typed model

Instead of searching through the Parameters list manually, you can bind the multipart form parameters to a strongly typed model the same way as for a regular query/form/JSON request. Inherit your model from MultipartModel (which exposes Files) and add your own properties:

public class UploadModel : MultipartModel
{
    public string Title { get; set; }

    public int Count { get; set; }
}

The parameters are parsed into the model properties automatically (the same parser as Simplify.Web query/form binding is reused, so [BindProperty], [Exclude], [Format], IList<T> properties and validation attributes are all supported), while the uploaded files remain accessible via Model.Files:

public class MyController : Controller2<UploadModel>
{
    public async Task<ControllerResponse> Invoke()
    {
        Model.Title; // bound from the "Title" multipart parameter
        Model.Count; // bound from the "Count" multipart parameter
        Model.Files; // uploaded files
    }
}

The legacy MultipartViewModel (which exposes the raw Parameters list) still works as before and now also inherits from MultipartModel.