[英]Can't get response from ASP.NET web API
我正在设置一个ASP.net Web API,但是我一直从所有Http路由中获取404。
它运行在http:// localhost:52011 /上,因为这是VS2013使用的默认位置。 我添加了一个简单的测试HttpRoute,如果使用GET调用它来测试连通性,它只会返回一个字符串。
已启用目录浏览,以反馈Web API实际上位于localhost上,并且项目的所有目录均可见。
我尝试设置在WebApiConfig.cs的路由中固定的控制器名称,并阅读了很多有关HttpRoute映射的信息,但是我一直收到404或“找不到与名为controllername的控制器匹配的类型”。
我正在使用Postman进行测试,因为这是推荐的且易于使用。
欢迎任何提示!
WebApiConfig.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace BadgePrinter
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
//route to print badge.
config.Routes.MapHttpRoute(
name: "apiPrint",
routeTemplate: "api/{controller}/{action}/{Bedrijf}/{Aanspreektitel}/{Voornaam}/{Achternaam}/{Jobtitle}/{Kopies}/{Rijksregisternummer}/{BadgeAfdruk}/{printer}/{Image}",
defaults: new { Image = RouteParameter.Optional}
);
//test route to get string back
config.Routes.MapHttpRoute(
name: "apiTest",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
BadgeController.cs(项目中唯一的控制器)
第一种方法是我最终必须使用的方法,但是测试方法是我用于测试的方法。
using BadgePrinter.CardTemplates;
using BadgePrinter.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace BadgePrinter.Controllers
{
public class BadgeController : ApiController
{
//removed the first method since it was big and distracting.
// test method
[HttpGet]
public IHttpActionResult GetAllProducts()
{
return Ok("some product");
}
}
}
我正在使用的测试方法的链接: http:// localhost:52011 / api / BadgeController / Products / 1
我也尝试了此链接,即使那没关系: http:// localhost:52011 / api / BadgeController / Products / 1
结果:
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:52011/api/BadgeController/Products/1'.",
"MessageDetail": "No type was found that matches the controller named 'BadgeController.cs'."
}
对于其他的HttpRoute,我一直得到404,但是如果我可以进行测试,我将无处可去:) PS:我对这种事情(ASP.net和API)是陌生的,所以我很抱歉如果我是个白痴。
您在这里有2个问题:
1)您的控制器名称BadgeController
但在您的API中,它应仅为Badge
。 正确的URL应该是:
http://localhost:52011/api/Badge/xxx
2)在您的代码中看不到方法名称Products
或类似名称。
3)如果是POST
方法,则参数输入中必须具有[FromBody]
。
您的网址存在一些问题:
http://localhost:52011/api/BadgeController/Products/1
据我所知,您没有任何GET方法可以映射到ID参数。 您的控制器中只有
[HttpGet]
public IHttpActionResult GetAllProducts()
不接受任何输入。
2)记录在案的Web API约定是您没有在URL中明确编写“ Controller”。 在这种情况下,仅需要控制器唯一的第一部分( Badge
)。
3)我看不到您从哪里获得URL的“产品”部分。 没有自定义的路线定义或属性会成为路线的一部分。 再次约定是,您定义一个动作方法,该动作方法的名称以“ Get”开头,并且将映射到控制器名称的GET
请求,并且末尾没有其他内容。 该方法的其余名称无关紧要-在您的情况下, AllProducts
始终与/Products
不匹配...因此,即使有必要,它仍然是不正确的。
总之,访问您的GET操作的正确URL应为:
http://localhost:52011/api/Badge/
注意:如果要更改现有的GetAllProducts方法,或添加类似以下的第二种方法:
[HttpGet]
public IHttpActionResult Get(int id)
{
return Ok("Product" + id);
}
然后,您可以在URL中使用ID,例如
http://localhost:52011/api/Badge/1
它应该路由到此方法。
尝试删除网址的控制器部分:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.