簡體   English   中英

.NET API POST 端點未被擊中

[英].NET API POST Endpoint not getting hit

我有一個.net api ,我想從控制台應用程序測試 api。 我要測試的方法是POST方法。我將控制台應用程序中的數據序列化為 json 字符串,我想將其發布到 API,但 API 沒有被命中,我的控制台應用程序也沒有收到任何錯誤.

我的GET調用雖然有效。 這只是我無法上班的職位。

我的 API 控制器->

using _ErrorLogger.Shared;
using _ErrorLogger.Server.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Runtime.CompilerServices;

namespace _ErrorLogger.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ExceptionDetailsController : ControllerBase
    {
        private readonly IExceptionDetailsService _exceptionDetailsService;
        public ExceptionDetailsController(IExceptionDetailsService exceptionDetailsService)
        {
            _exceptionDetailsService = exceptionDetailsService;
        }

        [HttpGet]
        [Route("GetExceptions")]
        public async Task<List<ExceptionDetails>> GetAll()
        {
            return await _exceptionDetailsService.GetAllExceptionDetails();
        }

        [HttpGet]
        [Route("GetExceptionByID/{id}")]
         public async Task<ExceptionDetails> GetByID(int id)
        {
            return await _exceptionDetailsService.GetExceptionDetails(id);
        }

        [HttpPost]
        [Route("CreateException")]
        public async Task<IActionResult> CreateException([FromBody]string obj)
        {
            //await _exceptionDetailsService.AddExceptionDetails(exceptionDetails);
            return Ok();
        }

        [HttpPost]
        [Route("Test")]
        public async Task<IActionResult> Test([FromBody] string obj)
        {
            return Ok();
        }
    }
}

來自控制台應用程序的我的呼叫 ->

public async void ExceptionsAnalyzer(Exception exception)
        {
            HttpClient _httpClient = new HttpClient();

            StackTrace stack = new StackTrace(exception, true);

            StackFrame frame = stack.GetFrame(stack.FrameCount - 1);

            ExceptionDetails exceptionDetails = new ExceptionDetails
            {
                ExceptionMessage = exception.Message,
                InnerException = exception.InnerException?.ToString(),
                ExceptionType = exception.GetType().ToString(),
                ExceptionSourceFile = frame.GetFileName(),
                ExceptionSourceLine = frame.GetFileLineNumber().ToString(),
                ExceptionCaller = frame.GetMethod().ToString(),
                ExceptionStackTrace = exception.StackTrace,
                DateLogged = DateTime.Now

            };

            string json = JsonSerializer.Serialize(exceptionDetails);
            //var stringContent = new StringContent(json, Encoding.UTF8, "application/json");

            HttpResponseMessage response = await _httpClient.PostAsJsonAsync("http://localhost:5296/api/ExceptionDetails/CreateException", json);
            if (response.IsSuccessStatusCode)
            {

            }
        }

我期待 api 端點被擊中。

我期待 api 端點被擊中。

那么,首先,您在控制台應用程序中的方法是ExceptionsAnalyzer結構是錯誤的。 它應該是static類型,因為控制台應用程序本身的main methodstatic類型。

另一個錯誤是async應該是Task類型,並且在調用ExceptionsAnalyzer方法時它應該是wait()以進行響應,但是您的控制台應用程序是 static 那么它將如何處理await調用? 所以請看下面的解決方案:

解決方案:

    using System.Net.Http.Json;
    using System.Text.Json;
    
  // Calling method   
    ExceptionsAnalyzer().Wait();
  //Defining Method in dotnet 6 console app    
    static async Task ExceptionsAnalyzer()
    {
        HttpClient _httpClient = new HttpClient();
        var obj = "Test data";
        string json = JsonSerializer.Serialize(obj);
        HttpResponseMessage response = await _httpClient.PostAsJsonAsync("http://localhost:5094/api/ExceptionDetails/CreateException", json);
        if (response.IsSuccessStatusCode)
        {
    
        }
    }

注意:我沒有考慮你的參數Exception exception ,你可以自己修改。 我主要考慮的是為什么你不能到達 API 端點。 希望你現在弄錯了。

Output:

在此處輸入圖像描述

除非ExceptionDetails是您的基本路徑的一部分並且因此包含在所有 API 調用中,否則我認為您需要刪除它。

您將調用的route定義為CreateException ,因此 url 應該是<base url>/CreateException

如果這沒有幫助,請發布整個 controller 的代碼(使用端點方法)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM