Web Api在运行时返回状态代码500

西蒙妮西班牙

我在asp.net核心中开发的Web API出现问题。当我在POST中调用它们时,出现以下错误:

ebbtelemetrywebapi.azurewebsites.net/api/events:1无法加载资源:>服务器的状态为500(内部服务器错误)

控制器功能如下:

    [Route("api/[controller]")]
    [ApiController]
    public class EventsController : ControllerBase
    {
      [HttpPost]
      public async Task<IEnumerable<CosmosDBEvents>> 
      GetAsync(EventsGetTwoParamsDto dto)
      {
         try
            {
                switch(dto.Action2)
                {
                    case "Index":
                    case "Pagina5":
                        const string VERIFICATION_CODE = "where doc.deviceId = \"{0}\"";
                        var content = string.Format(VERIFICATION_CODE,dto.DeviceIdorId);

                        var items = await DocumentDBRepository<CosmosDBEvents>.GetItemsAsync(content);

                        if (items == null || items.Count() == 0)
                        {
                            return null;
                        }
                        MessagesController messageController = new MessagesController(_context);
                        EventsTypeDescriptionsController eventController = new EventsTypeDescriptionsController(_context);

                        int codice_evento;
                        string cultura;

                        foreach (var item in items)
                        {
                            codice_evento = Convert.ToInt32(item.eventId);
                            cultura = GetCulture();
                            item.decodifica_evento = messageController.GetMessageWithCulture(codice_evento, cultura);
                            item.descrizione_evento = eventController.GetDetail(codice_evento);
                        }
                        return (items);
                    case "BottoneLedOff":
                        content = string.Format(VERIFICATION_CODE, dto.DeviceIdorId);

                        items = await DocumentDBRepository<CosmosDBEvents>.GetItemsAsync(content);

                        if (items == null || items.Count() == 0)
                        {
                            return null;
                        }
                        items = (from item in items
                                 orderby item.startTS descending
                                 select item).Take(1);

                        var service = ServiceClient.CreateFromConnectionString(AppSettings.KeyIoT);
                        var methodInvocation = new CloudToDeviceMethod("GetData") { ResponseTimeout = TimeSpan.FromSeconds(200) };
                        var response = await service.InvokeDeviceMethodAsync(dto.DeviceIdorId, methodInvocation);

                        CosmosDBTelemetry realtime = JsonConvert.DeserializeObject<CosmosDBTelemetry>(response.GetPayloadAsJson());

                        if(realtime.severity<90)
                        {
                            ((List<CosmosDBEvents>)items)[0].BLedAcceso = true;
                        }
                        else
                        {

                            ((List<CosmosDBEvents>)items)[0].BLedAcceso = false;
                        }

                        return (items);
                    case "Dettagli":
                        // in questo caso il deviceid è l'id dell'evento eventId
                        if (dto.DeviceIdorId == null)
                        {
                            return null;
                        }

                        items = await DocumentDBRepository<CosmosDBEvents>.GetItemAsync(dto.DeviceIdorId);
                        var events = items.FirstOrDefault();


                        codice_evento = Convert.ToInt32(events.eventId);
                        cultura = GetCulture();


                        messageController = new MessagesController(_context);
                        events.decodifica_evento = messageController.GetMessageWithCulture(codice_evento, cultura);

                        eventController = new EventsTypeDescriptionsController(_context);
                        events.descrizione_evento = eventController.GetDetail(codice_evento);

                        if (events == null)
                        {
                            return null;
                        }
                        //ritorno una lista di un elemento
                        List<CosmosDBEvents> toRet = new List<CosmosDBEvents>
                        {
                            events
                        };
                        return (toRet);
                    default:
                        return null;
                }
            } catch(Exception ex)
            {
                throw (ex);
            }
        }
   }

dto类声明如下:

    public class EventsGetTwoParamsDto
    {
        public string DeviceIdorId { get; set; }
        public string Action2 { get; set; }
    }

AJAX调用如下:

     $.ajax({
        type:"POST",
        url: ENV.apiUrl+'/events',
        data: JSON.stringify({ DeviceIdorId: ENV.deviceId, Action2: 
    "Pagina5"}),
        dataType: "json",
        contentType: 'application/json;charset=UTF-8',
        success: function(data){
            console.log("RISPOSTA", data);
        }
      });

我有以下异常:

Microsoft.AspNetCore.Mvc.Internal.AmbiguousActionException:匹配多个动作。以下操作与路线数据匹配并满足所有约束:

我看一下AmbiguousActionException:匹配了多个动作。以下操作与路线数据匹配并满足所有约束,但我的情况有些不同。在我的控制器中,我有两个GetAsync方法,一个带有两个参数,另一个带有三个参数。我相信这是问题的根源。在您提到的帖子中,没有这种情况,最重要的是POST方法。

这是控制器类:

[Route("api/[controller]")]
[ApiController]
public class EventsController : ControllerBase
{

    private readonly TelemetryWebContext _context;

    public EventsController(TelemetryWebContext context)
    {
        _context = context;
    }

    [HttpPost]
    public async Task<IEnumerable<CosmosDBEvents>> GetAsync([FromBody]EventsGetTwoParamsDto dto)
    {
    }

    [HttpPost]
    public async Task<IEnumerable<CosmosDBEvents>> GetAsync([FromBody]EventsGetThreeParamsDto dto)
    {
    }

    private string GetCulture() => "en-GB";
  }

可以帮我吗?

西蒙妮西班牙

我通过使用HttpPost重载解决了为控制器的第二种方法提供新的URL段的问题

例如

[HttpPost("xyz")]

我感谢CodeNotFound给他的帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在运行时为使用wsimport生成的代码重写或设置Web服务端点

在运行时更新Java代码

如何在运行时在启用Spring的Web应用中安排新方法?

相当于Azure Web App列表运行时的Azure REST API

从Web Api控制器返回HTTP状态代码

Web API在运行时条件式序列化属性

在运行时修改功能代码

如何在运行时为OData Web API建立EDM模型?

在web.config中requireQuestionAndAnswer设置为false,但是RequiresQuestionAndAnswer在运行时为true

如何在运行时更改Web服务地址?

模块化ASP.NET Web API:如何在运行时向Web API添加/删除路由

为什么Visual Studio 2017在运行时会重置.NET Core web.config文件

状态为0的响应:URL:null –在模拟器或Web中运行时出现此错误

Java Web Start无法找到Java运行时

QProcess:进程(Web浏览器)仍在运行时被销毁

Web应用程序:在运行时删除文件

代码在运行时挂起

在运行时更改代码

(Java)在运行时动态创建新的REST Web服务

为什么getenv()仅在从终端运行时才返回变量,而不是通过Web服务器返回变量

在运行时以Windows形式C#移动Web浏览器

ASP.Net Web Api 如何在运行时更改令牌过期时间

在运行时加载 BingMaps API

如何在 Web API 中使用 AutoFac 在运行时解析服务?

在运行时添加状态

在运行时放心的 API 编排

在运行时保存当前状态

获取 web api 的控制器返回状态代码

将我的 .NET 6 Web API 作为自包含服务运行时如何防止 404?