gpt4 book ai didi

web-services - Ajax请求期间收到 "no type was found that matches the controller named"错误消息

转载 作者:行者123 更新时间:2023-12-03 22:56:50 26 4
gpt4 key购买 nike

我已经看过很多有关此问题的主题,但不幸的是,我相信每个案例都是不同的案例(或大多数情况),并且我真的很希望得到一些专家对我的案例的意见,特别是因为我无法发表自己的意见即使在阅读了其他一些主题后,代码仍然可以工作。

情况:我正在 jQuery 中使用 Ajax 请求调用我在 WebApi 项目中与 MVC 4 应用程序一起创建的 WebService 方法。

我的 WebService Controller 类看起来像默认的,如下所示:

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 请求如下所示:

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 文件如下所示:

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 中返回的数据变量时,此错误消息不断出现,内容如下:

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

如果有人能尽快帮助我,我将非常感激。提前致谢!

最诚挚的问候,
疯狂

最佳答案

查看错误,看起来 Web API 无法找到 Controller “类型”AdditionalInfo。 Web API 使用程序集解析器扫描程序集并找出 Controller 类型。在您的情况下,由于某种原因,它无法找到您的“AdditionalInfo” Controller ,可能是因为加载具有此 Controller 的程序集时出现一些问题。

尝试以下操作并查看您的事件日志中是否记录了任何错误。如果您发现任何错误,那么您可能应该检查您的 Controller 是否存在于这些程序集中。

  • 在 Web.config 中进行以下更改以查看 EventLog 中的错误

    <system.diagnostics>
    <trace autoflush="false" indentsize="4">
    <listeners>
    <add name="myListener"
    type="System.Diagnostics.EventLogTraceListener"
    initializeData="WebApiDiagnostics" />
    </listeners>
    </trace>
    </system.diagnostics>

  • 在 WebApiConfig.cs 中,您可以执行以下操作:

       IAssembliesResolver assembliesResolver = config.Services.GetAssembliesResolver();

    ICollection<Assembly> assemblies = assembliesResolver.GetAssemblies();

    StringBuilder errorsBuilder = new StringBuilder();

    foreach (Assembly assembly in assemblies)
    {
    Type[] exportedTypes = null;
    if (assembly == null || assembly.IsDynamic)
    {
    // can't call GetExportedTypes on a dynamic assembly
    continue;
    }

    try
    {
    exportedTypes = assembly.GetExportedTypes();
    }
    catch (ReflectionTypeLoadException ex)
    {
    exportedTypes = ex.Types;
    }
    catch (Exception ex)
    {
    errorsBuilder.AppendLine(ex.ToString());
    }
    }

    if (errorsBuilder.Length > 0)
    {
    //Log errors into Event Log
    Trace.TraceError(errorsBuilder.ToString());
    }

    顺便说一句,上面的一些代码实际上来自 Web API 用来解析 Controller 类型的 DefaultHttpControllerTypesResolver。 http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/Dispatcher/DefaultHttpControllerTypeResolver.cs

已编辑:另一种可能遇到此问题的情况是,如果您的 Controller 嵌套在另一个类中。这是一个错误,后来修复了。

关于web-services - Ajax请求期间收到 "no type was found that matches the controller named"错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16673851/

26 4 0