有没有办法使用VBA从Excel向asp.net mvc控制器httppost动作方法发送数据

时间:2017-08-02 14:20:41

标签: asp.net-mvc excel-vba post vba excel

我的控制器是:

[HttpPost]
public IActionResult Updtdb(string longStr)
{
//some code here!
}

我正在尝试从excel向我的网站发送数据。 这是我到目前为止所尝试的内容。

Sub XMLPost()
Dim xmlhttp, longStr As String

    With ActiveSheet
        longStr = "x=" & generateData
    End With

    Set xmlhttp = CreateObject("microsoft.xmlhttp")

    With xmlhttp
        .Open "POST", " http://localhost:54188/DashBoard/Updtdb", False
        .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        .send longStr
    End With
End Sub

Function generateData()
    Dim longString As String, range As range

    Set range = Sayfa8.range("SummaryTable")

    For Each cell In range.Cells
        longString = longString + CStr(cell.Value) + ";"
    Next cell

    generateData = longString
End Function

当我运行Sub XML Post()时,它可以工作但不会调用action方法。我不确定如何实现它或甚至可以实现。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了这个问题。我的项目是使用asp.net身份验证。 DashBoardController仅适用于经过身份验证的用户。我已经为我的动作控制器添加了一个数据注释。现在一切正常。

  

[使用AllowAnonymous]

[HttpPost]
[AllowAnonymous]
public ActionResult Updtdb(string x)
{
    return Content("a");
}