Web Api 2错误请求 [英] Web Api 2 bad request

查看:75
本文介绍了Web Api 2错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web api的初学者,我正在尝试设置一个简单的owin自托管服务,我正在尝试该服务.

Im a beginner with Web api and Im trying to setup a simple owin selfhosted service that Im trying out.

我一直在搜索stackoverflow和其他地方已有一段时间了,但是我似乎找不到任何明显错误的东西.

I've been searching stackoverflow and other places for a while now, but I cant seem to find anything obviously wrong.

我的问题是每次尝试致电服务时都会收到错误的请求响应.

The problem I have is that I get a bad request response everytime I try to call my service.

控制器看起来像这样:

 [RoutePrefix("api/ndtdocument")]
public class NDTDocumentsController : ApiController, INDTDocumentsController
{
    [HttpGet]
    public IHttpActionResult Get()
    {
        var document = Program.NDTServerSession.GetNextNDTDocument(DateTime.Today);
        if (document == null)
            return null;

        return Ok(document);


    }

    [Route("")]

    public IHttpActionResult Post([FromBody] NDTDocument ndtDocument)
    {
        try
        {
            Program.NDTServerSession.AddNDTDocument(ndtDocument);
            return Ok();
        }
        catch(Exception ex)
        {
            return BadRequest(ex.Message);

        }


    }





}

客户端看起来像这样:

     static void Main(string[] args)
    {


         AddNDTDocument(@"C:\Testing.txt");


    }



    private static void AddNDTDocument(string centralserverPath)
    {

        var client = GetServerClient();
        NDTDocument document = new NDTDocument();
        var response = client.PostAsJsonAsync("ndtdocument", document).Result;


    }

    static HttpClient GetServerClient()
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:9000/api/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        return client;



    }

当我调试它时,我可以看到请求uri实际上是 http://localhost:9000/api/ndtdocument

I can see when I debug it that the request uri is infact http://localhost:9000/api/ndtdocument

但是响应始终是错误的请求,并且我在控制器中有一个断点,并且永远不会调用它.

But the response is allways bad request and I have a breakpoint in the controller and it is never invoked.

每次我尝试使用Web api做某事时,我总是遇到一些奇怪的问题(但很简单的问题).

Everytime I try to do something with web apis I Always run into some weird (but simple problem).

有什么想法吗?

谢谢!

推荐答案

Web API将根据您的方法名称决定您的路由.由于您已经在类级别添加了[RoutePrefix("api/ndtdocument")],因此这将是通往控制器的路由.当Web api查找操作时,它将与方法名称匹配,因此,在您的情况下,您的实际路由为http://localhost:9000/api/ndtdocument/post.

Web API will decide your route based on your method names. Since you have added [RoutePrefix("api/ndtdocument")] on class level this will be the route to your controller. When web api looks for an action it will match on method names, so in your case your actual route would be http://localhost:9000/api/ndtdocument/post.

在尝试确定特定操作需要Web api的http method时,将检查您的方法名称,以post开头的方法为http postgethttp get等.

When trying to decide what http method that a specific action requires web api will check your method names and methods starting with post will be http post, get will be http get etc.

所以可以说我们改为调用方法PostData,对于初学者来说,我们可以删除[HttpPost]属性.我们的路线现在是http://localhost:9000/api/ndtdocument/postdata.现在让我们说,我们希望我们的路径只是/data.然后,我们首先将方法重命名为Data,但是现在Web api不知道我们要使用哪个http方法来调用此方法,这就是为什么要添加[HttpPost]属性.

So lets say we would instead call our method PostData, for starters we could remove the [HttpPost] attribute. Our route would now be http://localhost:9000/api/ndtdocument/postdata. Let's now say that we want our path to be just /data. We would then first rename our method to Data, but now web api does not know what http method we want to invoke this method with, thats why we add the [HttpPost] attribute.

阅读评论后进行编辑

[Route("{id:int}")]
public IHttpActionResult Get(int id)

[Route("")]
public IHttpActionResult Post([FromBody] NDTDocument ndtDocument)

这篇关于Web Api 2错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆