【问题标题】:WebApi endpoint not getting hit when deployed on IIS server, working fine in local host部署在 IIS 服务器上时,WebApi 端点未命中,在本地主机上工作正常
【发布时间】:2023-03-08 12:52:01
【问题描述】:

解决方案部署到 IIS 服务器时,Web API 端点未命中

工作端点: https://localhost:44335/Api/Course/GetStudents

端点不工作 http://192.168.1.3:9090/Api/Course/GetStudents

Ajax 代码

 function GetStudents(IsEdit) {
    $.ajax({
        url: "/Api/Course/GetStudents",
        type: "GET",
        dataType: 'json',
        success: function (data) {
            $.each(data, function (i, obj) {
                $('#ddlStudent').append($('<option>').text(obj.Student_Name).attr('value', obj.Student_Id));
            });

            if (IsEdit === true) {
                $('#ddlStudent').val(SelectedSupplier).select2();
            }
            else {
                GetCourses(false);
            }
        }

    });
}

Web API 控制器

        [HttpGet]
        [Route("GetStudents")]
        public string GetStudents()
        {
            string JsonString = String.Empty;

            DBPROC dBPROC = new DBPROC(configuration);

            DataTable Dt = dBPROC.GetDataTable("SP_GetStudents");
            if (Dt.Rows.Count > 0)
            {
                JsonString = JsonConvert.SerializeObject(Dt);
            }

            return JsonString;

        }
 

【问题讨论】:

  • 请求的响应码是什么?这应该可以让您了解问题所在。
  • @Rory McCrossan 响应状态为 500
  • 好的,所以它被击中了,但是有一个内部服务器错误?数据库连接是否正确,SP_GetStudents 是否存在?顺便说一句,作为旁注,你不应该在你的存储过程前加上 SP_
  • @Christain 存储过程存在代码在本地主机上工作正常并按预期返回 json 数据,在 IIS 上部署后无法正常工作
  • 我想你可以使用 Try/catch 块,并将错误消息和堆栈跟踪作为字符串返回,至少你会知道什么是失败的。

标签: jquery ajax .net-core asp.net-core-webapi


【解决方案1】:

您可以尝试将代码包装在 try/catch 块中,并将错误作为字符串返回。

    [HttpGet]
    [Route("GetStudents")]
    public string GetStudents()
    {
        string JsonString = String.Empty;

        try {

        DBPROC dBPROC = new DBPROC(configuration);

        DataTable Dt = dBPROC.GetDataTable("SP_GetStudents");
        if (Dt.Rows.Count > 0)
        {
            JsonString = JsonConvert.SerializeObject(Dt);
        }
        }
        catch (Exception exception) {
          JsonString = $"Exception {exception.Message} and Stack: {exception.StackTrace}";
        }

        return JsonString;

    }

【讨论】:

  • 问题已解决.. 应用程序池身份验证错误.. 将识别属性更改为管理员用户名和密码.. 它可以工作.. 谢谢
  • 你是从 try/catch 块中得到的吗?
  • 是的 .. 正确是从 try catch 得到的
  • 如果有帮助,记得点赞?
猜你喜欢
  • 2018-03-07
  • 2018-10-22
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2020-06-08
  • 1970-01-01
  • 2013-05-21
相关资源
最近更新 更多