获取“未找到与名为的控制器匹配的类型"Ajax 请求期间的错误消息 [英] Getting the "no type was found that matches the controller named" error message during Ajax Request

查看:42
本文介绍了获取“未找到与名为的控制器匹配的类型"Ajax 请求期间的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多关于此的话题,但不幸的是,我相信每个案例都是不同的案例(或大多数案例),我真的很希望有专家对我的案例提出意见,因为我无法做出我的即使在阅读了其他一些主题后,代码也能正常工作.

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.

我的 WebService 控制器类看起来像默认的,像这样:

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

如果有人能尽快帮助我,我将不胜感激.提前致谢!

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 调用中数据"字段中的内容,我就为应用程序中的对象创建了一个类来保存整个数据.似乎由于某种原因,该方法不能具有语法Get(int 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)".

相反,我做了类似Get(object)"的操作,在 Ajax 请求中类似data: 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)很挑剔,我将方法的名称更改为其他名称(例如 Retrieve 或其他名称).

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

这篇关于获取“未找到与名为的控制器匹配的类型"Ajax 请求期间的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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