ASP.NET的WebAPI扔404,如果方法的参数是字符串或INT [英] ASP.NET WebAPI throw 404 if method parameter is string or int

查看:87
本文介绍了ASP.NET的WebAPI扔404,如果方法的参数是字符串或INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了ASP.NET MVC4的WebAPI一个非常简单的测试,并发现了一些有趣的问题:

I did a very simple test on ASP.NET MVC4 WebAPI and found some interesting issue:

当一个方法正在复杂类型,它会工作,但是当它需要字符串或整型,它会抛出404,给出的屏幕截图:在addProduct命令的作品,但测试和测试1始终是未找到。

When a method is taking complex type, it will work, but when it takes string or int, it will throw 404, as the screen shot given: The "AddProduct" works, but "Test" and "Test1" is always not found.

我应该如何正确地调用该方法?

How should I invoke the method correctly?

推荐答案

你有没有试过,

$.ajax({
  url : "/api/product/test",
  data : { username : "edi" },
  dataType : "json",
  type : "POST",
  success : function(res){ console.log(res); },
  error : function(req, stat, err){ console.log(stat + ": " + err); }
});

现在它的失败监守你在引号包裹着你的整个JSON对象(在jQuery的AJAX方法)。

Right now it's failing becuase you've wrapped your entire json object (in the jquery ajax method) in quotes.

尝试不带引号,让我知道!

Try without the quotes and let me know!

此外,

在测试单一变量,如字符串的用户名和int值大家注意到,WEB API会期望它完全一样的。

When testing single variables like string username and int value take a note that WEB API will expect it exactly like that.

这家伙,

[HttpPost]
public string Test1(int value) { ... }

将寻找此URL签名匹配后(IM使用HTTPIE)...

Will look for a post that matches this url signature (im using HTTPIE)...

$ HTTP POST http://yourwebsite.com/api/test1 值= 1

$ http POST http://yourwebsite.com/api/test1 value=1

其中4是在Test1的方法变量值的数值。

Where the "4" is the value of the variable "value" in that Test1 method.

更多关于HTTPIE这里:<一href=\"http://www.hanselman.com/blog/InstallingHTTPIEHTTPForHumansOnWindowsGreatForASPNETWebAPIAndRESTfulJSONServices.aspx\"相对=nofollow>斯科特Hanselman在上安装HTTPIE

More about HTTPIE here: Scott Hanselman on installing HTTPIE

希望帮助!

这篇关于ASP.NET的WebAPI扔404,如果方法的参数是字符串或INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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