Mad*_*tsu 5 jquery web-services asp.net-web-api
我已经看过很多关于这方面的话题,但不幸的是我相信每个案例都是一个不同的案例(或大多数案例),我真的很想得到一些专家对我的案例的意见,因为我不能让我的代码工作甚至阅读完一些其他主题后.
情况:我在jQuery中使用Ajax请求调用我在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 = "";
}
}
Run Code Online (Sandbox Code Playgroud)
来自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!");
});
}
Run Code Online (Sandbox Code Playgroud)
我的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 }
);
}
}
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的是,这是我的问题:当我在.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'."
}"
Run Code Online (Sandbox Code Playgroud)
如果有人能尽快帮助我,我将非常感激.提前致谢!
最好的问候,
疯狂
Kir*_*lla 12
查看错误看起来Web API无法找到控制器'类型'AdditionalInfo.Web API使用程序集解析程序扫描程序集并找出控制器类型.在你的情况下由于某种原因,它无法找到你的'AdditionalInfo'控制器可能是因为加载具有此控制器的组件有一些问题.
请尝试以下操作,查看EventLog中是否记录了任何错误.如果您发现任何错误,那么您可能应检查这些组件中是否存在控制器.
在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());
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,上面的一些代码实际上来自DefaultHttpControllerTypesResolver,Web API使用它来解析控制器类型. http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Http/Dispatcher/DefaultHttpControllerTypeResolver.cs
编辑:如果你的控制器嵌套在另一个类中,你可以解决这个问题的另一个场景.这是一个后来修复的错误.
好吧,所以我相信我知道发生了什么事。我不完全确定,但至少我的问题得到了解决。
只需更改 Ajax 调用中“数据”字段内部的内容,我就为应用程序中的对象创建了一个类来保存整个数据。似乎由于某种原因该方法不能具有语法“Get(int ID)”。
相反,我做了类似“Get(object)”的操作,并在 Ajax 请求中执行了类似“data: obj.ID”的操作,瞧,它起作用了。
另外,由于框架对 REST 方法(Get、Post、Put 和 Delete)的名称很挑剔,因此我将该方法的名称更改为其他名称(例如 Retrieve 或其他名称)。
希望这对将来的人也有帮助。
最好的问候,
疯狂
| 归档时间: |
|
| 查看次数: |
19048 次 |
| 最近记录: |