1

I have a .Net Core 3.1 web application API

and every night I need to run a schedule

how can I launch a function like this one from the command line ?

// POST: api/Partners/export
[HttpPost]
[Route("export")]
public async Task<ActionResult> ExportLeads(SinglePost singlePost)
{
    ...

with dotnet run ? I cannot find any info about this, I don't even know if it is possible

thanks

phil123456
  • 1,192
  • 2
  • 10
  • 26
  • If you want to run a schedule then you can create a windows service project and make an API request. This will be good and easy. – parthraj panchal Nov 19 '20 at 08:11
  • What do you mean by calling an action from the command line? How to make an HTTP call to it? This has nothing to do with .NET itself, you could use `curl` or any similar tool to execute an HTTP GET request, eg `curl http://www.example.org:1234/mycontroller/myaction` – Panagiotis Kanavos Nov 19 '20 at 09:07
  • @parthrajpanchal there's no need for a custom service just to schedule a job. Windows provides Scheduled Tasks for this. – Panagiotis Kanavos Nov 19 '20 at 09:11
  • Does this answer your question? [How do I POST JSON data with cURL?](https://stackoverflow.com/questions/7172784/how-do-i-post-json-data-with-curl) – Panagiotis Kanavos Nov 19 '20 at 09:12
  • @Panagiotis Kanavosa schedule needs to run every night, I need to call a function localy, not from http...I know how to schedule something, but how do I call a .NET controller API function ????? – phil123456 Nov 19 '20 at 09:39
  • @phil123456 "locally" means from inside the web app. You aren't calling this from the web app. Everything outside the app uses HTTP, that's the whole point of creating a web app – Panagiotis Kanavos Nov 19 '20 at 09:41
  • @phil123456 what are you trying to do? Why create a web app in the first place if you don't want it to accept HTTP calls? If you want a scheduled job to call an action, what's wrong with making an HTTP call? – Panagiotis Kanavos Nov 19 '20 at 09:43
  • @Panagiotis Kanavosa hence my question, how to do so ? now if there is no way to call a web api controller from a local (command line/batch context), then how do I proceed ? I dont feel like copy pasting all my db models/controllers to a standalone windows app project for instance – phil123456 Nov 19 '20 at 09:44
  • That's not local. That's external. You're trying to make an external call. Your application doesn't handle command-line arguments though, it only accepts HTTP calls. If want to handle command-line arguments you'll have to explicitly add code in your `Main` method for that. Again, why are you using a web app for this? If the web app is already running, why not make an HTTP call? If not .... why create a web app if it's not running? – Panagiotis Kanavos Nov 19 '20 at 09:56
  • If you need a separate command-line tool that only performs this export, you need to extract the common code into a separate class library and use it from both the web app and a new command-line project – Panagiotis Kanavos Nov 19 '20 at 09:57
  • sh...precisely what I wanted to avoid – phil123456 Nov 19 '20 at 12:20

0 Answers0