【问题标题】:WebApi HttpPost not working on Windows AzureWebApi HttpPost 无法在 Windows Azure 上运行
【发布时间】:2014-04-09 11:20:11
【问题描述】:

我正在开发一个使用 WebApi2 和 MVC 以及 Angular 和 D3 api 的示例项目。我的 WebApi 遇到问题。使用 Azure 数据库连接字符串在本地计算机上一切正常,但是当我在 Azure 上发布相同内容时,我的 HttpPost 停止工作,而 HttpGet 工作正常。

[HttpPost]
[Route("api/dashboard/addnewassignment")]
public WebApiD3Sample.ViewModels.CoursePersonAssignmentModel AddNewCoursePersonAssignment([FromBody]WebApiD3Sample.ViewModels.CoursePersonAssignmentModel model) 
{
     if (ModelState.IsValid) {
          var modelAfterSave = AssignmentService.AddAssignment(model);
          return modelAfterSave;
     }
     ModelState.AddModelError("Invalid", "Not a Valid Save");
     return model;
}

我在发布时面临的错误

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 
http://sampledataweb.azurewebsites.net/api/dashboard/addnewassignment
    Object
    message: "An error has occurred."
__proto__: Object

【问题讨论】:

标签: c# azure asp.net-web-api


【解决方案1】:

我可以向您的网址发送帖子:

Status Code: 200 OK
Access-Control-Allow-Headers: Origin,X-Requested-With,Content-Type,Accept
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Content-Encoding: gzip
Content-Length: 123
Content-Type: application/json; charset=utf-8
Date: Thu, 06 Mar 2014 18:48:25 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/8.0
Set-Cookie: ARRAffinity=dbb5756ce35e0494cf70c90b9aba80f70f92f607fb3ebb3e7dffe4ecc1aba24a;Path=/;Domain=sampledataweb.azurewebsites.net WAWebSiteSID=696c72c37b2e472b90f6033923558edd; Path=/; HttpOnly
Vary: Accept-Encoding
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

所以问题出在您的 CoursePersonAssignmentModel 或 AssignmentService.AddAssignment 方法中。安装一个 nuget 来记录(即 Elmah),它将帮助您捕获错误。另一个不错的选择,您可以使用 Intellitrace 进行调试:http://blogs.msdn.com/b/zainnab/archive/2013/02/12/understanding-intellitrace-part-i-what-the-is-intellitrace.aspx

【讨论】:

    【解决方案2】:

    这似乎不是跨域问题,但只是为了确保您在 web.config 中允许它吗?

    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <remove name="Access-Control-Allow-Origin" />
                <remove name="Access-Control-Allow-Headers" />
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Headers" value="Origin,X-Requested-With,Content-Type,Accept" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2012-11-12
    • 2012-12-12
    • 1970-01-01
    • 2015-04-26
    相关资源
    最近更新 更多