当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Posting Form Data to MVC Controller and getting 404 Not Found Error

问题描述:

I have a MVC project in C# and in one of my controllers I am accepting HTTP POST data from an outside source and trying to save the file. I have tried many different method signatures but I am having a hell of time figuring out which one to use. I know they don't work because I am getting a 404 error in my IIS logs everytime I try to post to the URL.

A few things I have tried are

public ActionResult Index(FormCollection forms)

public ActionResult Index(FormCollection forms, HttpPostedFileBase files)

public ActionResult Index()

I am writing the request out to a failed request log to try and get more insight and I see the headers are:

Content-Length: 158293970

Content-Type: multipart/form-data; boundary=--------------------9228c56ead9fc97c

Accept-Encoding: gzip

Host: myhostinformationhere

User-Agent: SecurityCenter/5.3.1 (201604012350)

Any suggestions?

Some more information I have found. The code posting to my site is php code using curl. Without revealing too much of the vendor source code I see a couple of things they are doing which may help solve the problem. The headers code is

$sendHeaders = array ("Except:","X-SecurityCenter: $uuid", "Accept: application/html; charset=utf-8",);

They are also adding post fields like this

$postFields['reportType'] = $reportType;

$postFields['reportContent'] = curl_file_create($pathInfo['filepath'],$pathInfo['type'],basename($pathInfo['filepath']));

I have updated my code to the following but I am getting a timeout error now in my logs

public ActionResult Index(HttpPostedFileBase upload)

{

if (upload != null && upload.ContentLength > 0)

{

var fileName = Path.GetFileName(upload.FileName);

var path = Path.Combine(Server.MapPath(~/Files/Uploads"), fileName);

upload.SaveAs(path);

}

return View();

}

So with the above code I have confirmed with fiddler that a file is captured and uploaded to the correct directory. Now to figure out how to get the post to work from the client software.

网友答案:

If I'm understanding this right you should add another Action in your Controller. You shouldn't use the Index action to try and post data. Try something like this.

[HttpPost]
public ActionResult Save( HttpPostedFileBase file )
{
   if( file != null )
   {
   }

   // Return something to signify success/failure
}
网友答案:

You may add another Action in your Controller. You shouldn't use the Index action to try and post data. Index Action is used only for showing data, not for posting data form.

My code

[HttpPost]
public ActionResult Save( HttpPostedFileBase file )

{

   if( file != null )

   {

   }

return view();

   // Return something to signify success/failure

}
网友答案:

Here are a few things you can try.

Try an action method like this:

public ActionResult Index(HttpPostedFileBase files)
{
    // your work...
}

Next thing you need to do is to inspect the Request Body of the request; this should be in your IIS log. Copy that body and then use Fiddler to create that exact request. Here is a tutorial on how to make a post request using Fiddler.

In your MVC project's Global.asax.cs file, put the following code:

/// <summary>
/// Handles the HttpApplication's Error event.
/// </summary>
protected void Application_Error() 
{
     // Put a breakpoint here to give you an idea of what is going wrong.
     Exception exception = this.Server.GetLastError();
}

You can also use MVC route debugger to help you out.

In conclusion, keep trying and get it to work with Fiddler with the exact request you are getting from the vendor. Once you have done so, try again with the vendor product.

You can also use Fiddler to capture the request that is coming from the vendor product and inspect the request using that as well.

分享给朋友:
您可能感兴趣的文章:
随机阅读: