情况:我在jQuery中使用Ajax Request调用我在WebApi项目中创建的WebService方法以及MVC 4应用程序.
我的WebService控制器类看起来像默认值,如下所示:
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,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'." }"
如果有人能尽快帮助我,我将非常感激.提前致谢!
最好的祝福,
狂
@H_404_26@解决方法
尝试以下操作,查看EventLog中是否记录了任何错误.如果您发现任何错误,那么您可能应检查这些组件中是否存在控制器.
>在Web.config中进行以下更改以查看EventLog中的错误
< System.Diagnostics程序>
< trace autoflush =“false”indentsize =“4”>
<听众>
< add name =“myListener”
类型= “System.Diagnostics.EventLogTraceListener”
initializeData =“WebApiDiagnostics”/>
< /听众>
< /跟踪>
< /system.diagnostics\u0026gt;
>在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()); }
顺便说一下,上面的一些代码实际上来自DefaultHttpControllerTypesResolver,Web API使用它来解析控制器类型.
http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/Dispatcher/DefaultHttpControllerTypeResolver.cs
编辑:您可以遇到此问题的另一种情况是您的控制器是否嵌套在另一个类中.这是一个后来修复的错误.
@H_404_26@ @H_404_26@ 原文链接:https://www.f2er.com/html/232152.html