【问题标题】:Testing REST API through Postman is throwing Reference error通过 Postman 测试 REST API 会引发参考错误
【发布时间】:2020-06-25 15:18:09
【问题描述】:

我在 Visual Studio 2019 的 .net 核心和控制器类中创建了这个 REST API,我有以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RecLoadAPI.DAL;
namespace RecLoadAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RecLoadPrimeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [Route("RecLoadPrime/insertRecLoadData/{RecStartDate}/{RecEndDate}")]
        [HttpPost]
        public void insertRecLoadData(string RecStartDate, string RecEndDate)
        {
            RecLoadDataProvider dataProvider = new RecLoadDataProvider();
            dataProvider.InsertCardsData(RecStartDate, RecEndDate);
        }
    }
}

我正在尝试通过 Postman 测试我的代码,我写了这个 URL:

https://localhost:44306/api/RecLoadPrime/insertRecLoadData/?RecStartDate=01/01/2020&RecEndDate=01/02/2020

在邮递员中,我不断收到错误消息,即未定义名称。我不确定我做错了什么。我对 REST API 很陌生。下面是 Postman 的截图。

下面是我的 PreRequest 脚本的图片:

Pre Request 脚本现在完全是空的。我现在得到一个不同的错误:

HTTP Error 500.30 - ANCM In-Process Start Failure

以下是错误:

<!DOCTYPE html>
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8" />
    <title> HTTP Error 500.30 - ANCM In-Process Start Failure </title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;
            font-size: .813em;
            color: #222;
            background-color: #fff;
        }

        h1,
        h2,
        h3,
        h4,
        h5 {
            /*font-family: 'Segoe UI',Tahoma,Arial,Helvetica,sans-serif;*/
            font-weight: 100;
        }

        h1 {
            color: #44525e;
            margin: 15px 0 15px 0;
        }

        h2 {
            margin: 10px 5px 0 0;
        }

        h3 {
            color: #363636;
            margin: 5px 5px 0 0;
        }

        code {
            font-family: Consolas, "Courier New", courier, monospace;
        }

        body .titleerror {
            padding: 3px 3px 6px 3px;
            display: block;
            font-size: 1.5em;
            font-weight: 100;
        }

        a {
            color: #1ba1e2;
            text-decoration: none;
        }

        a:hover {
            color: #13709e;
            text-decoration: underline;
        }

        li {
            margin: 5px;
        }
    </style>
</head>

<body>
    <h1> HTTP Error 500.30 - ANCM In-Process Start Failure </h1>

    <h2> Common causes of this issue: </h2>
    <ul>
        <li> The application failed to start </li>
        <li> The application started but then stopped </li>
        <li> The application started but threw an exception during startup </li>
    </ul>

    <h2> Troubleshooting steps: </h2>
    <ul>
        <li> Check the system event log for error messages </li>
        <li> Enable logging the application process' stdout messages </li>
        <li> Attach a debugger to the application process and inspect </li>
    </ul>

    <h2>
        For more information visit:
        <a href="https://go.microsoft.com/fwlink/?LinkID=2028265">
            <cite> https://go.microsoft.com/fwlink/?LinkID=2028265 </cite></a>
    </h2>

</body>

</html>

我们将不胜感激。

【问题讨论】:

  • PreRequest Script 中有什么代码?
  • 向我们展示您的预请求脚本。问题与您的应用无关。 Postman 中的 Pre-request 脚本有错误(Pre-request 脚本选项卡)
  • 在邮递员中试试这个:https://localhost:44306/api/RecLoadPrime/insertRecLoadData/01%2F01%2F2020/01%2F02%2F2020 你需要对日期进行编码,而且你的 url 也有太多内容。
  • @kristech 错误说: Pre-request 脚本出错!这意味着 Postman 中 Pre-request script 选项卡上的 Pre-request script 有错误
  • 曾评论过您将 Query 和 Url 参数混合在一起,但正如@RomanMarusyk 指出的那样,这不是您当前的问题。不过需要注意的地方

标签: c# rest api


【解决方案1】:

首先,如果你有[Route("api/[controller]")],那么你不需要控制器名称(RecLoadPrime[Route("RecLoadPrime/insertRecLoadData/{RecStartDate}/{RecEndDate}")] 因为 URL 将是

https://localhost:44306/api/RecLoadPrime/RecLoadPrime/insertRecLoadData

那么,如果是POST请求,最好使用带有JSON的请求体而不是路由/查询参数。但这取决于你。

如果您想在 URL 中传递日期,那么您有 2 个选项

  1. 使用查询字符串:[Route("RecLoadPrime/insertRecLoadData")] 那么 URL 将是 http://localhost/api/RecLoadPrime/insertRecLoadData?RecStartDate=01/01/2020&amp;RecEndDate=01/02/2020

  2. 使用路由参数:[Route("RecLoadPrime/insertRecLoadData/{RecStartDate}/{RecEndDate}")],那么你需要在发送http://localhost/api/RecLoadPrime/insertRecLoadData/01%2F01%2F2020/01%2F02%2F2020之前对值进行编码,并在控制器中解码。

See this answer 修复:

HTTP 错误 500.30 - ANCM 进程中启动失败

【讨论】:

    猜你喜欢
    • 2021-01-12
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2020-05-27
    • 1970-01-01
    • 2018-10-02
    • 2017-10-08
    相关资源
    最近更新 更多