ulinq 0.1.0
dotnet add package ulinq --version 0.1.0
NuGet\Install-Package ulinq -Version 0.1.0
<PackageReference Include="ulinq" Version="0.1.0" />
paket add ulinq --version 0.1.0
#r "nuget: ulinq, 0.1.0"
// Install ulinq as a Cake Addin
#addin nuget:?package=ulinq&version=0.1.0
// Install ulinq as a Cake Tool
#tool nuget:?package=ulinq&version=0.1.0
ulinq
ULinq is a .NET standard library that converts MVC/WebAPI controller actions into URLs. It is particularly useful for integration testing.
Take for example the following controller:
[Route("api/v1/[controller])]
public class WeatherController : Controller{
[HttpGet]
public Task<int> GetCurrentTemperatureByPostalCode(int postalCode){
return Task.FromResult(85);
}
}
Our goal is to refactor this integration test:
[Theory]
[InlineData(19106,85)]
public async Task GetWeatherForPostalCode(int postalCode, int expectedResult){
var response = await Client.GetAsync($"/api/v1/Weather?postalCode={postalCode}");
var json = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<int>(json);
data.Should().Equal(expectedResult);
}
Into this:
[Theory]
[InlineData(19106,85)]
public async Task GetWeatherForPostalCode(int postalCode, int expectedResult){
var route = RouteHelper.GetRoute<WeatherController, int>(controller => controller.GetCurrentTemperatureByPostalCode(postalCode));
var response = await Client.GetAsync(route.ToString());
var json = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<int>(json);
data.Should().Equal(expectedResult);
}
Customization
For most scenarios, the default model binders should be able to map your action parameters back to a URI. However, in some cases, like custom model binders, you will need to write a bit of code to help out.
First create the route binder:
/// <summary>
/// Pulls an id from an inbound model and sets it on the route
/// </summary>
/// <seealso cref="AbstractRouteBinder" />
public class CustomFromModelRouteBinder : AbstractRouteBinder
{
/// <summary>
/// Determines whether this instance can bind the specified parameter.
/// </summary>
/// <param name="parameter">The parameter.</param>
/// <returns>
/// <c>true</c> if this instance can decompose the specified parameter; otherwise, <c>false</c>.
/// </returns>
public override bool CanBind(IControllerActionParameter parameter)
{
var model = parameter.ParameterValue as MyModel;
return model != null;
}
/// <summary>
/// Binds the parameter to the route.
/// </summary>
/// <param name="parameter">The parameter.</param>
/// <param name="controllerActionRoute">The controller action route.</param>
protected override void BindParameter(IControllerActionParameter parameter, IControllerActionRoute controllerActionRoute)
{
var model = parameter.ParameterValue as MyModel;
controllerActionRoute.SetRouteValue("id", model.PersonId);
}
}
Now let's register our binder.
ControllerActionParameterBinders.AddBinders(new CustomFromModelBinder());
That's it! Enjoy.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.1
- Microsoft.AspNetCore.Mvc.Core (>= 2.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|
Initial Release 0.1.0