Skip to content
Rico Suter edited this page Sep 4, 2015 · 23 revisions

TODO: Update code samples (currently not valid)

From controller in precompiled Web API assembly

TODO

<#@ template debug="false" hostspecific="true" language="C#" #>

<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.CodeGeneration.dll" #>

<#@ import namespace="NSwag.CodeGeneration" #>

<#@ import namespace="System.IO" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="Microsoft.CSharp" #>

<#@ output extension=".ts" #>
<#
	// CONFIGURATION
	var assemblyPath = @"../../bin/NSwag.Demo.Web.dll";
	var controllerClass = "NSwag.Demo.Web.Controllers.PersonsController";
	var urlTemplate = "api/{controller}/{action}/{id}";
	// -------------
	
	var fullAssemblyPath = Path.GetFullPath(Path.GetDirectoryName(Host.TemplateFile) + assemblyPath); 
	
	var controllerType = Assembly.LoadFrom(fullAssemblyPath).GetType(controllerClass);
	var NSwagGenerator = new NSwagWebApiServiceGenerator(urlTemplate);
	var service = NSwagGenerator.Generate(controllerType);

	// TODO: Add client code generation
#>
<#= code #>

TODO

From a NSwag file in the project

TODO

<#@ template debug="false" hostspecific="true" language="C#" #>

<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.CodeGeneration.dll" #>

<#@ import namespace="NSwag.CodeGeneration" #>

<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.CSharp" #>

<#@ output extension=".ts" #>
<#
	// CONFIGURATION
	var filePath = @"../../ServiceDefinitions/MyService.NSwag";
	// -------------
	
	var fullFilePath = Path.GetFullPath(Path.GetDirectoryName(Host.TemplateFile) + filePath); 
	var service = NSwagService.FromJson(File.ReadAllText(fullFilePath));

	// TODO: Add client code generation
#>
<#= code #>

TODO

From a NSwag served from a remote server

To generate the client code from the NSwag from a remote server, use the following code:

<#@ template debug="false" hostspecific="true" language="C#" #>

<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\NSwag.CodeGeneration\bin\Debug\NSwag.CodeGeneration.dll" #>

<#@ import namespace="NSwag.CodeGeneration" #>

<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.CSharp" #>

<#@ output extension=".ts" #>
<#
	// CONFIGURATION
	var url = @"http://localhost:22093/api/Persons/NSwag";
	// -------------
	
	var service = NSwagService.FromUrl(url);

	// TODO: Add client code generation
#>
<#= code #>

Only the URL to where the NSwag definition is served is required.

Generate the client code

TypeScript

Required namespaces:

<#@ import namespace="NSwag.CodeGeneration.TypeScript" #>

Required code:

var codeGenerator = new TypeScriptNSwagServiceGenerator(service);

var provider = new CSharpCodeProvider();
var className = Path.GetFileNameWithoutExtension(Host.TemplateFile); 
codeGenerator.Class = provider.CreateEscapedIdentifier(className);

var code = codeGenerator.GenerateFile();

CSharp

TODO