本文介绍了在邮递员中测试web api 2 MVC 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hI all

我是asp.net web api的新手2

我为我的控制器/ api创建了api并填充了以下(1 *)

我想用chrome上的postman测试api

但是这给我404错误

这是我的webapconfig(2 *)

,这是我用来邮递员的网址:localhost:2383 / api / products

这是邮递员的错误(3 *)

帮助我,如果你可以

谢谢。



我尝试过:



1 *



hI all
I am new to asp.net web api 2
I created api to my controllers/api and populated that with following (1*)
I want to test the api with postman on chrome
but this give me 404 error
this is my webapconfig (2*)
and this is the url that I used to postman : localhost:2383/api/products
and this is the error on postman(3*)
help me if you can
thanks.

What I have tried:

1*

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using My_World.Models;

namespace My_World.Controllers.Api
{
    public class ProductsController : ApiController
    {
        Product[] products = new Product[]
        {
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
        };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }


        public IHttpActionResult GetProduct(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }
    }
}





2 *



2*

<using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

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

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



3 *

[HttpException]:路径控制器'/没有找到api / products'或者没有实现IController。

在System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext,Type controllerType)

在System.Web .Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,String controllerName)

at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext,IController& controller,IControllerFactory& factory)

在System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)

在System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,AsyncCallback回调,对象状态) $ b的
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously)


3*
[HttpException]: The controller for path '/api/products' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

推荐答案

public IEnumerable<Product> Get()
        {
            return products;
        }





然后你可以做/ api /产品,如果你做了





Then you could do /api/products and if you did

public IEnumerable<Product> Get(int id)
        {
            return products.Where(m=>m.Id == id);
        }





您可以使用/ api / products / 1.



您的另一个选择是使用路由将URL重命名为您想要的。您可以通过属性或RouteConfig.cs文件声明此路由。



You could use /api/products/1.

Your other option is to use routes to rename your URL's to however you want. You can declare this routes via attributes or in your RouteConfig.cs file.


这篇关于在邮递员中测试web api 2 MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:11