开发者

Call asp mvc Controller Action from another application in .net?

I need to run a Control开发者_StackOverflow中文版ler Action from my Console Application that reference to my MVC App.

The reason behind this is because there are some special Action method that i cannot just simply copy to the new application. (Because of the control things)

So I just want to ask if there could be any way to call a controller action method from the Console application? How could I send a file upload to the controller?

This is the example of my ActionResult that used to upload a file? how should i send the Request.Files to it?

  public ActionResult ImageUp(){
        foreach (string fname in Request.Files)
        {
            HttpPostedFileBase _file = Request.Files[fname];
        }
  }


I am pretty sure you can but it will take a bit of work to plug everything in correctly. You can create an instance of the controller class and call the ImageUp method from that instance. To setup the Request.Files correctly you will need to create your own instance of ControllerContext in order to push the values you want into Request.Files.


System.Net.WebClient.UploadFile() can upload a file to a URL.

If you wanted to either upload multiple files, or to have other non-file parameters in the request, see the accepted answer to this question Upload files with HTTPWebrequest (multipart/form-data)

That said, a better solution would be to refactor ImageUp() into 2 methods - the first extracts the files from the HttpRequest, and the second acts on those files. The second method is then usable without a HttpContext, such as in a console app or unit test.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜