T4 Web API C # Class Library in Typescript

I recently asked about C # classes for T4 view modes, but that's different, so I separated it into a second question

I would like to automate the creation of a client library in typescript that includes endpoints for each web api method, which is decorated with the attributes HttpGet, Post, Put, Delete, Patch.

This will result in a jQuery ajax call to interact with web api calls.

Has anyone seen anything like this? I LOVE to save time to hack a T4 script.

Thanks!

+6
source share
3 answers

Looks like this guy used T4 to create a TypeScript proxy for his web API service. http://galador.net/codeblog/post/2013/11/12/Client-Side-Web-Application-primer.aspx

However, if your only client is from the browser, you might want to use SignalR instead of this T4 template. https://gist.github.com/robfe/4583549

There are also several libraries that generate TypeScript definition files from CLR types:

+5
source

Try using Weld: https://weld.codeplex.com/

All you have to do is add an attribute to the action of your controller, and Weld will create a typescript proxy.

On its website:

Instead:

[HttpGet] public int Sum(int x,int y) { return x + y; } 

and using $ .ajax

  var url = "/Home/Sum"; var data = { x: 2 , y: 3}; $.ajax({ url: url, data: data, success: showResult }); 

Just add the attribute:

 [AjaxMethod] 

And do it in your typescript:

 HomeController.prototype.Sum(2, 3, showResult); 
+3
source

I am currently working on a toolchain to solve this exact problem: create client code to invoke the web interface controller.

Generators can be called in the T4 template, in the cmd line, and in the Windows GUI:

enter image description here

Check out http://nswag.org

0
source

Source: https://habr.com/ru/post/956204/


All Articles