简体   繁体   English

从.net中的另一个应用程序调用asp mvc Controller Action?

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

I need to run a Controller Action from my Console Application that reference to my MVC App. 我需要从控制台应用程序中运行引用我的MVC应用程序的Controller Action。

The reason behind this is because there are some special Action method that i cannot just simply copy to the new application. 这背后的原因是因为有一些特殊的Action方法,我不能简单地将其复制到新应用程序中。 (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? 这是我用来上传文件的ActionResult的示例吗? how should i send the Request.Files to it? 我应该如何发送Request.Files到它?

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

System.Net.WebClient.UploadFile () can upload a file to a URL. System.Net.WebClient.UploadFile ()可以将文件上传到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) 如果您要上传多个文件,或在请求中包含其他非文件参数,请参见此问题的可接受答案, 使用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. 也就是说,更好的解决方案是将ImageUp()重构为2种方法-第一种方法从HttpRequest中提取文件,第二种方法对那些文件进行操作。 The second method is then usable without a HttpContext, such as in a console app or unit test. 然后,第二种方法无需HttpContext就可以使用,例如在控制台应用程序或单元测试中。

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. 您可以创建控制器类的实例,然后从该实例调用ImageUp方法。 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. 为了正确设置Request.Files,您需要创建自己的ControllerContext实例,以便将所需的值推送到Request.Files中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET | 从 MVC 中的 controller 调用另一个控制器的操作 - ASP.NET | Call another controller's action from a controller in MVC ASP.NET MVC AJAX 调用应用程序/控制器/操作? - ASP.NET MVC AJAX call Application/Controller/Action? ASP.NET MVC 4从另一个控制器调用一个控制器方法 - ASP.NET MVC 4 call a controller method from another controller 调用另一个控制器,并对调用的控制器操作MVC ASP.NET使用不同的布局 - Call another controller and use a different Layout for the called controller action MVC ASP.NET 将数据从一个控制器动作传递到ASP.Net MVC 5中的另一个控制器动作 - pass data from one controller action to another controller action in ASP.Net MVC 5 无法从其他控制器MVC 4调用操作 - Can not call action from another controller MVC 4 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller ASP.NET MVC以编程方式初始化Controller并调用操作 - ASP.NET MVC initialize Controller and call action programatically ASP.NET MVC从同一控制器发布到控制器操作 - ASP.NET MVC post to controller action from same controller 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM