简体   繁体   English

从 Postman 调用时未执行 API 方法

[英]API Method not being executed when called from Postman

I'm making an API in .NET Framework.我正在 .NET Framework 中制作 API。

I have a controller called Autos, the API is meant to allow for the registration and retrieval of car models.我有一个名为 Autos 的控制器,该 API 旨在允许注册和检索汽车模型。

At first, I was able to use a GET to fetch an IEnumerable instance of List from Postman.起初,我能够使用 GET 从 Postman 获取 List 的IEnumerable实例。 I was getting the serialized version of a list I made with dummy data.我得到了我用虚拟数据制作的列表的序列化版本。

However, after I removed the dummy list, changed the method return to HttpResponseMessage and switched to a static dictionary I'd be using as a repository, the method stopped being executed upon calling it with Postman.但是,在我删除了虚拟列表,将方法返回到HttpResponseMessage并切换到我将用作存储库的静态字典后,该方法在使用 Postman 调用时停止执行。 I added logging messages and breakpoints and I can see that they just never execute.我添加了日志消息和断点,我可以看到它们永远不会执行。

My controller:我的控制器:

    public class AutosController : ApiController
        {
            [HttpGet]
            public HttpResponseMessage GetModels(HttpRequestMessage request)
            {
//Theres a breakpoint here
                System.Diagnostics.Debug.WriteLine(Repository.AutoRepository.Keys);
                return request.CreateResponse<IEnumerable<Auto>>(HttpStatusCode.OK, Repository.AutoRepository.Values);
            }
            [HttpPost]
            public HttpResponseMessage SetModel(HttpRequestMessage request, Auto serializedAuto)
            {
                if (serializedAuto.IsValid())
                {
                    if (Repository.AddToRepository(serializedAuto))
                        return request.CreateResponse<Auto>(HttpStatusCode.Created, Repository.AutoRepository[serializedAuto.Id]);
                    else
                        return request.CreateResponse(HttpStatusCode.BadRequest);
                } else
                {
                    return request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }
        }

And my API Route还有我的 API 路由

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/v1/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

I can successfully use SetModel and I can confirm that the sent model is also added to the repository.我可以成功使用 SetModel,并且可以确认发送的模型也已添加到存储库中。 I am also able to return it to postman.我也可以将其退还给邮递员。

The postman address I'm using for the GET is我用于 GET 的邮递员地址是

localhost:51020/api/v1/GetModels

the way i see it, your action could be在我看来,你的行为可能是

 [Route("~/api/v1/GetModels")]
public IEnumerable<Auto> GetModels()
  {

    var values = Repository.AutoRepository.Values;
    if (values==null) throw new HttpException(404, "Some description");
    return values;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM