简体   繁体   English

无法从ASP.NET Web API获得响应

[英]Can't get response from ASP.NET web API

I'm setting up an ASP.net web API, but i keep getting 404 from all Http routes. 我正在设置一个ASP.net Web API,但是我一直从所有Http路由中获取404。

This is running on http://localhost:52011/ since that is the default location that VS2013 uses. 它运行在http:// localhost:52011 /上,因为这是VS2013使用的默认位置。 I've added a simple test HttpRoute that just returns a string if it's called with a GET to test connectivity. 我添加了一个简单的测试HttpRoute,如果使用GET调用它来测试连通性,它只会返回一个字符串。

Directory browsing is enabled for feedback that the web API is in fact on localhost, and all directories of the project are visible. 已启用目录浏览,以反馈Web API实际上位于localhost上,并且项目的所有目录均可见。

I've tried setting the controller name fixed in the routes in WebApiConfig.cs and read a lot about the HttpRoute mappings, but i keep getting either a 404 or a "No Type was found that matches the controller named controllername ". 我尝试设置在WebApiConfig.cs的路由中固定的控制器名称,并阅读了很多有关HttpRoute映射的信息,但是我一直收到404或“找不到与名为controllername的控制器匹配的类型”。

I'm using Postman to test since that was recommended and easy to use. 我正在使用Postman进行测试,因为这是推荐的且易于使用。

Any tips are welcome! 欢迎任何提示!

WebApiConfig.cs: 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(the only controller in the project) BadgeController.cs(项目中唯一的控制器)

The first method is the one i'm going to have to use eventually, but the testmethod is the one i use for testing. 第一种方法是我最终必须使用的方法,但是测试方法是我用于测试的方法。

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");
        }
    }
}

link for the testmethod that i'm using: http://localhost:52011/api/BadgeController/Products/1 我正在使用的测试方法的链接: http:// localhost:52011 / api / BadgeController / Products / 1

I also tried this link, even though that shouldn't matter: http://localhost:52011/api/BadgeController/Products/1 我也尝试了此链接,即使那没关系: http:// localhost:52011 / api / BadgeController / Products / 1

Result: 结果:

{
    "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'."
}

For the other HttpRoute, i keep getting 404, but i'll be in heaven if i can get the test to work :) PS: i'm new to this kind of thing(ASP.net and API's) so i'm sorry if i'm being an idiot. 对于其他的HttpRoute,我一直得到404,但是如果我可以进行测试,我将无处可去:) PS:我对这种事情(ASP.net和API)是陌生的,所以我很抱歉如果我是个白痴。

You have 2 issues here: 您在这里有2个问题:

1) Your controller name BadgeController but in your API it should be Badge only. 1)您的控制器名称BadgeController但在您的API中,它应仅为Badge The correct URL should be: 正确的URL应该是:

http://localhost:52011/api/Badge/xxx

2) I can't see method name Products or something like that in your code. 2)在您的代码中看不到方法名称Products或类似名称。

3) If it is a POST method, it must have [FromBody] in parameter input. 3)如果是POST方法,则参数输入中必须具有[FromBody]

There are some problems with your URL: 您的网址存在一些问题:

http://localhost:52011/api/BadgeController/Products/1 

As far as I can see you don't have any GET method which would map to an ID parameter. 据我所知,您没有任何GET方法可以映射到ID参数。 All you have in your controller is 您的控制器中只有

[HttpGet]
public IHttpActionResult GetAllProducts()

which doesn't accept any inputs. 不接受任何输入。

2) The documented Web API convention is that you don't write "Controller" explicitly in the URL. 2)记录在案的Web API约定是您没有在URL中明确编写“ Controller”。 Just the first part which is unique to the controller ( Badge ) in this case is required. 在这种情况下,仅需要控制器唯一的第一部分( Badge )。

3) I can't see where you are getting the "Products" part of the URL from. 3)我看不到您从哪里获得URL的“产品”部分。 There's no custom route definition or attribute which would make that part of the route. 没有自定义的路线定义或属性会成为路线的一部分。 Again the convention is that you define an action method whose name starts with "Get" and that will map to a GET request to the controller name, with no further bits and pieces on the end. 再次约定是,您定义一个动作方法,该动作方法的名称以“ Get”开头,并且将映射到控制器名称的GET请求,并且末尾没有其他内容。 The rest of the method's name is irrelevant - and in your case AllProducts doesn't match to /Products anyway...so even if that was necessary it would still be incorrect. 该方法的其余名称无关紧要-在您的情况下, AllProducts始终与/Products不匹配...因此,即使有必要,它仍然是不正确的。

In conclusion, the correct URL to access your GET action should be: 总之,访问您的GET操作的正确URL应为:

http://localhost:52011/api/Badge/

NB If you were to either change your existing GetAllProducts method, or add a second method like this: 注意:如果要更改现有的GetAllProducts方法,或添加类似以下的第二种方法:

[HttpGet]
public IHttpActionResult Get(int id)
{
  return Ok("Product" + id);
}

Then you could use the ID in your URL, eg 然后,您可以在URL中使用ID,例如

http://localhost:52011/api/Badge/1

and it should route to this method. 它应该路由到此方法。

Try dropping the controller part of the url: 尝试删除网址的控制器部分:

http://localhost:52011/api/Badge/Products/1 http:// localhost:52011 / api / Badge / Products / 1

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

相关问题 无法从 ASP.NET Web API 获得任何响应 - Can't get any response from ASP.NET Web API 在 Asp.net web API 和 Z03D476861AFD384510F2CB081 上使用 localhost 时无法得到响应 - Can't get a response back when using localhost on Asp.net web API and postman 如何从Asp.Net Web Api 2获得类似于Google API响应的JSON结果? - How can I get JSON result from Asp.Net Web Api 2 that looks like Google API response? 尝试从asp.net Web API调用获取响应时,Android Studio中出现超时异常 - Timeout exception in android studio while trying to get a response from an asp.net web api call 如何从 ASP.NET Core Web API 向客户端发送 JSON 响应? - How can I send JSON response to client from ASP.NET Core Web API? 继承Controller时无法使Route在ASP.Net Web API 2中工作 - Can't get Route to work in ASP.Net Web API 2 when inheriting Controller 无法使CORS适用于ASP.NET Core Web API - Can't get CORS working for ASP.NET Core Web API Sending class data as JSON array format for GET request Response in ASP.Net Dot Core Web API ( GET response data from Web API) - Sending class data as JSON array format for GET request Response in ASP.Net Dot Core Web API ( GET response data from Web API) 从ASP.NET Web API获取可反序列化的DateTime - Get deserializable DateTime from ASP.NET Web API Angular 4-如何从ASP.Net Web API获取数据 - Angular 4 - How to get data from ASP.Net web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM