简体   繁体   English

在 ASP.Net MVC 项目中无法访问 HttpPost 方法

[英]HttpPost method can't be reached, in ASP.Net MVC Project

Abstract:抽象的:
I have an Asp.net MVC project, and a console windows app.我有一个 Asp.net MVC 项目和一个控制台 windows 应用程序。
I try to have the console app to post a file to the web project through a [HttpPost] method in a controller.我尝试让控制台应用程序通过 controller 中的 [HttpPost] 方法将文件发布到 web 项目。
Both my projects are running locally, each one in its own visual studio instance.我的两个项目都在本地运行,每个项目都在自己的 visual studio 实例中。
My problem is that my [httppost] method is never called.我的问题是我的 [httppost] 方法从未被调用过。

Details:细节:
Here is the code in the web project这是web项目中的代码

public class DevController : Controller  
{  
    [HttpPost]  
    public ActionResult UploadStuff(HttpPostedFileBase file)  
    {
        System.Diagnostics.Debugger.Break(); //This breakpoint is never reached
        return View();
    }
}

Here is the code in the console app:这是控制台应用程序中的代码:

static public async void UploadStuff(Stream streamToUploadToWebProject)
{
    var content = new MultipartFormDataContent();
    content.Add(new StreamContent(streamToUploadToWebProject), "WhateverName");
    var httpClient = new HttpClient();
    string url = @"http://localhost:12345/dev/UploadStuff";
    var response = await httpClient.PostAsync(url, content);
}

Notes:笔记:
1) http://localhost:12345/: This is the url that is displayed in browser when I run my web project. 1)http://localhost:12345/:这是我运行我的web项目时在浏览器中显示的url。
2) The file that is sent is an unstructured blob. 2) 发送的文件是一个非结构化的 blob。
3) Most related posts I have found are about uploading a json file (like this post), or are about posting a file from a web page (in which case there is a also "Get" method returning a view containing a "upload" button); 3)我发现的大多数相关帖子都是关于上传 json 文件(如这篇文章),或者是关于从 web 页面发布文件(在这种情况下,还有一个“获取”方法返回包含“上传”的视图按钮); Both cases are not same as mine.这两种情况都与我的不同。
4) [Edit] HttpClient.PostAsync returns a "404" error. 4) [编辑] HttpClient.PostAsync 返回“404”错误。

My Question:我的问题:
What do I do wrong;我做错了什么; how should I write the [HttpPost] method, so that it gets called?我应该如何编写 [HttpPost] 方法,以便它被调用?

In your controller try to use [FromBody] attribute before the parameter.在您的 controller 中,尝试在参数前使用 [FromBody] 属性。

[HttpPost]  
public ActionResult UploadStuff([FromBody]HttpPostedFileBase file)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM