Last active
March 27, 2017 21:50
-
-
Save sirkirby/282161b1dd2dc97086719159ba2b4770 to your computer and use it in GitHub Desktop.
scriptcs web api w/ controller that lists directory contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.IO; | |
| using System.Reflection; | |
| using System.Web.Http; | |
| using System.Web.Http.SelfHost; | |
| using System.Web.Http.Dispatcher; | |
| public class ControllerResolver : DefaultHttpControllerTypeResolver | |
| { | |
| public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver) | |
| { | |
| return Assembly.GetExecutingAssembly().GetTypes() | |
| .Where(x => typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(x)).ToList(); | |
| } | |
| } | |
| var config = new HttpSelfHostConfiguration(new Uri("http://localhost:8090")); | |
| config.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver()); | |
| config.Routes.MapHttpRoute( | |
| name: "DefaultApi", | |
| routeTemplate: "api/{controller}/{id}", | |
| defaults: new { id = RouteParameter.Optional }); | |
| public class FileSystemController : ApiController | |
| { | |
| public string[] Get(string dir = @"D:\") | |
| { | |
| var fileEntries = Directory.GetFiles(dir); | |
| return fileEntries; | |
| } | |
| } | |
| new HttpSelfHostServer(config).OpenAsync().Wait(); | |
| Console.WriteLine("Listening..."); | |
| Console.ReadKey(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| var script = new Process(); | |
| script.StartInfo.FileName = "scriptcs"; | |
| script.StartInfo.CreateNoWindow = true; | |
| script.StartInfo.Verb = "runas"; | |
| script.StartInfo.Arguments = @"csharp-web-server-sample.csx"; | |
| script.Start(); | |
| var proxy = new Process(); | |
| proxy.StartInfo.FileName = "ngrok"; | |
| proxy.StartInfo.CreateNoWindow = true; | |
| proxy.StartInfo.Arguments = "http 8090"; | |
| proxy.Start(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <packages> | |
| <package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" /> | |
| <package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" /> | |
| <package id="Microsoft.AspNet.WebApi.SelfHost" version="5.2.2" targetFramework="net45" /> | |
| <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" /> | |
| </packages> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ngrok http 8090 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :: start from the cli | |
| scriptcs -install | |
| scriptcs csharp-web-server-sample.csx |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requirements
choco install scriptcsnpm i -g ngrok