ASP.NET core exampleThis example shows how to:
- Set up configuration options to add elements to the 51Degrees Pipeline. {"PipelineOptions": {"Elements": [{"BuilderName": "math"}]}}
- Add the configuration file to the server's configuration (if the file is not the usual appsettings.json). public class Startup{...public void ConfigureServices(IServiceCollection services){Configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory() + "/AppData").AddJsonFile("pipeline.json").Build();...
- Add builders and the Pipeline to the server's services. public class Startup{...public void ConfigureServices(IServiceCollection services){...services.AddSingleton<MathElementBuilder, MathElementBuilder;services.AddFiftyOne<PipelineBuilder>(Configuration);...
- Configure the server to use the Pipeline which has just been set up. public class Startup{...public void Configure(IApplicationBuilder app, IHostingEnvironment env){app.UseFiftyOne();...
- Inject the
IFlowDataProvider
into a controller.public class HomeController : Controller{private IFlowDataProvider _flow;public HomeController(IFlowDataProvider flow){_flow = flow;}...} - Use the results contained in the flow data to display something on a page view. public class HomeController : Controller{...public IActionResult Index(){var math = _flow.GetFlowData().Get(MathElement.math);ViewData["Message"] = $"{math.Operation} = {math.Result}";return View();}...
Controller
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2020 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Example_Website.Controllers
{
public class HomeController : Controller
{
private IFlowDataProvider _flow;
public HomeController(IFlowDataProvider flow)
{
_flow = flow;
}
public IActionResult Index()
{
var math = _flow.GetFlowData()?.Get(MathElement.math);
if (math != null)
{
ViewData["Message"] = $"{math.Operation} = {math.Result}";
}
else
{
ViewData["Message"] = "No 'FlowData' found. This is usually " +
"because the 51Degrees middleware component has not run, " +
"possibly due to being after some other middleware " +
"component that has blocked further execution " +
"(For example, requiring HTTPS).";
}
return View();
}
}
}
Startup
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2020 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
namespace Example
{
public class Startup
{
IConfiguration Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory() + "/AppData")
.AddJsonFile("pipeline.json")
.Build();
services.AddSingleton<MathElementBuilder, MathElementBuilder>();
services.AddFiftyOne<PipelineBuilder>(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseFiftyOne();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc(routes =>
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}"));
}
}
}