C# 计数未加载类型[ProjectName]。Web API项目上的WebAPI应用程序
我有一个使用Web.API的.Net Web应用程序。项目属性显示它正在使用.Net 4.5 在我的网页中,以下代码调用其中一个方法C# 计数未加载类型[ProjectName]。Web API项目上的WebAPI应用程序,c#,jquery,.net,C#,Jquery,.net,我有一个使用Web.API的.Net Web应用程序。项目属性显示它正在使用.Net 4.5 在我的网页中,以下代码调用其中一个方法 $.ajax({ type: 'POST', url: '/Appointments/GetMissingKeys', data: $('#mrnList').val()
$.ajax({
type: 'POST',
url: '/Appointments/GetMissingKeys',
data: $('#mrnList').val()
}).done(function() {
alert('done!');
}).fail(function (Status, errorThrown) {
alert('Error: ' + Status.status + ' - ' + Status.statusText);
}).always(function () {
alert('All done or not');
});
这反过来应该调用以下命令
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace MRNInput.Controllers
{
public class AppointmentController : ApiController
{
[AcceptVerbs("Post")]
[Route("Appointments/GetMissingKeys")]
HttpResponseMessage GetMissingAppointmentKeys([FromBody]String MRNList)
{
HttpResponseMessage resp = null;
resp = new HttpResponseMessage(HttpStatusCode.Accepted);
return resp;
}
}
}
Server Error in '/' Application.
--------------------------------------------------------------------------------
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'MRNInput.WebApiApplication'.
Source Error:
Line 1: <%@ Application Codebehind="Global.asax.cs" Inherits="MRNInput.WebApiApplication" Language="C#" %>
Source File: /global.asax Line: 1
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
当我检查status对象时,我得到一个内部服务器错误500,响应如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace MRNInput.Controllers
{
public class AppointmentController : ApiController
{
[AcceptVerbs("Post")]
[Route("Appointments/GetMissingKeys")]
HttpResponseMessage GetMissingAppointmentKeys([FromBody]String MRNList)
{
HttpResponseMessage resp = null;
resp = new HttpResponseMessage(HttpStatusCode.Accepted);
return resp;
}
}
}
Server Error in '/' Application.
--------------------------------------------------------------------------------
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'MRNInput.WebApiApplication'.
Source Error:
Line 1: <%@ Application Codebehind="Global.asax.cs" Inherits="MRNInput.WebApiApplication" Language="C#" %>
Source File: /global.asax Line: 1
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408
现在除了错误页面中的.Net版本之外,我不明白为什么它不能加载最后一个类。如果是错误中报告的.Net版本,我如何才能让它在正确的.Net版本4.5下运行呢
选择构建菜单,然后选择“清洁解决方案”选项
这将消除“无法加载类型[ProjectName].WebAPI应用程序在Web API项目上”错误-由于找不到约会/GetMissingKeys,现在要处理路由问题:进行以下更改:
namespace MRNInput.Controllers
{
public class AppointmentController : ApiController
{
[HttpPost, Route("api/Appointments/GetMissingKeys")]
public IHttpActionResult GetMissingAppointmentKeys([FromBody]string MRNList)
{
return Ok();
}
}
}
$.ajax({
type: 'POST',
url: '/api/Appointments/GetMissingKeys',
data: $('#mrnList').val() //ensure you have a value here,
datatype: "json" //<-- note the additional property
})
我还建议将此添加到startup.cs中,以处理自动驼峰套管
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().Configure<MvcOptions>(options =>
{
options.OutputFormatters.OfType<JsonOutputFormatter>()
.First()
.SerializerSettings
.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
}
注意,您的js api调用正在寻找约会,而您的控制器正在寻找约会。试试这个
[RoutePrefix("api/Appointments")]
public class AppointmentController : ApiController
{
[AcceptVerbs("Post")]
[Route("GetMissingKeys")]
HttpResponseMessage GetMissingAppointmentKeys([FromBody]String MRNList)
{
HttpResponseMessage resp = null;
resp = new HttpResponseMessage(HttpStatusCode.Accepted);
return resp;
}
}
如果你将你的api方法公开,会有什么不同吗?同样,这是正确的url,还是应该是api/Appoints/getmissingkeys?只是尝试了一下,仍然得到404错误。不确定这是否与页面位于站点的根目录而不是视图目录有关。出现错误的文本消息是{消息:未找到与请求URI匹配的HTTP资源。}找到与名为“约会”的控制器匹配的类型。}这是因为您的控制器是约会单数,而您调用的是约会复数。去掉api调用中的“s”。我投票关闭,因为OP用干净的解决方案解决了这个问题。Paul-请创建一个新问题,因为它找不到约会/GetMissingKey。如果你更新了你的问题,所有的答案都是无关的。