获取"没有类型,发现相匹配的控制器命名为" Ajax请求过程中的错误信息 [英] Getting the "no type was found that matches the controller named" error message during Ajax Request
问题描述
我已经看到了很多关于这个话题,但不幸的是我相信每个案例是一个不同的案例(或大多数),我真的很喜欢一些专家有关我在特定情况下的意见,因为我不能让我的code的工作,甚至通过一些其他主题看完。
I've seen a lot of topics about this, but unfortunately I believe that each case is a different case (or most of them), and I really would love some experts opinion about my case in particular since I cannot make my code work even after reading through some of the other topics.
情况:我使用jQuery中一个Ajax请求调用我在一个项目的WebAPI用MVC 4应用程序一起创建一个WebService方法
Situation: I am using an Ajax Request call in jQuery to a WebService method I have created in an WebApi project together with a MVC 4 Application.
我的web控制器类貌似默认情况下,像这样的:
My WebService controller class looks like the default, like this:
public class AdditionalInfoController : ApiController
{
//GET api/AdditionalInfo
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
//GET api/AdditionalInfo/5
public string Get(int id)
{
return "value";
}
//PUT api/AdditionalInfo/5
public void Put(int id)
{
string test = "";
}
}
这是我的jQuery Ajax请求如下:
My Ajax Request from jQuery looks like this:
function GetAdditionalInfo(obj)
{
var request = jQuery.ajax({
url: "/api/AdditionalInfo/Get",
type: "GET",
data: { id: obj.id },
datatype: "json",
async: false,
beforeSend: function () {
},
complete: function () {
}
})
.done(function (a,b,c) {
alert("Additional info was retrieved successfully!");
})
.fail(function (a,b,c) {
alert("An error happened while trying to get the additional info!");
});
}
我WebAPIConfig文件看起来像这样:
My WebAPIConfig file looks like this:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
和最后但并非最不重要的,这是我的问题:此错误消息不断出现,当我浏览返回的数据变量.fail,这是什么写的是:
And last but not least, this is my problem: this error message keeps appearing when I browse the returned data variable in .fail and this is what is written:
"{
"Message":"No HTTP resource was found that matches the request URI 'http://localhost:59096/api/AdditionalInfo/Get?id=1'.",
"MessageDetail":"No type was found that matches the controller named 'AdditionalInfo'."
}"
我真的AP preciate,如果有人可以帮助我尽快。在此先感谢!
I would really appreciate it if someone could help me as soon as possible. Thanks in advance!
最好的问候,
结果
疯狂
Best regards,
Mad
推荐答案
好了,我相信我发现了什么事。我不能完全肯定,但至少我的问题得到了解决。
Ok, so I believe I found out what was going on. I am not entirely certain, but at least my problem got fixed.
只需改变什么是Ajax调用数据领域内,我创建了一个类为应用程序来保持整个数据的对象。似乎由于某种原因该方法不能有语法获取(中间体ID),
Simply by changing what was inside of the "data" field in the Ajax call and I have created a class for an object in the application to hold the whole data. It seems that for some reason the method could not have the syntax "Get(int ID)".
相反,我不喜欢的东西获取(对象),并在Ajax请求的东西等。数据显示:obj.ID瞧,它的工作
Instead, I did something like "Get( object)" and in the Ajax Request something like "data: obj.ID" and voila, it worked.
此外,由于框架是挑剔的REST方法(GET,POST,PUT和DELETE)的名字,我改变了方法,别的东西的名称(如检索或东西)。
Also, since the framework is picky about the names of the REST methods (Get, Post, Put and Delete), I changed the name of the method to something else (like Retrieve or something).
希望这会帮助别人,将来也是如此。
Hopefully this will help someone in the future as well.
最好的问候,
结果疯狂
Best regards,
Mad
这篇关于获取&QUOT;没有类型,发现相匹配的控制器命名为&QUOT; Ajax请求过程中的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!