C# 在web API MVC 4中找不到资源错误
我的web api看起来工作正常,它以json格式显示数据C# 在web API MVC 4中找不到资源错误,c#,.net,asp.net-mvc-4,asp.net-web-api,restful-architecture,C#,.net,Asp.net Mvc 4,Asp.net Web Api,Restful Architecture,我的web api看起来工作正常,它以json格式显示数据 http://localhost:60783/api/employee/GetEmployeeList/ 但当我试图指出.cshtm文件时,它会给我: The resource cannot be found error . 我的routeConfig看起来像这样 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
http://localhost:60783/api/employee/GetEmployeeList/
但当我试图指出.cshtm文件时,它会给我:
The resource cannot be found error .
我的routeConfig看起来像这样
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
还有我的webApiConfig:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
my Global.asax:
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
我有一个名为EmployeeController
我正在使用
public async Task<ActionResult> Index()
{
HttpResponseMessage response = await ServiceAccess.Get("Employee/GetEmployeeList", null);
if (response.IsSuccessStatusCode)
{
List<Employee> model = await ServiceAccess.DeserializeAs<List<Employee>>(response);
return View(model);
}
return Content("No Record");
}
public ActionResult EmployeeList()
{
return View();
}
感谢您的帮助。您的代码中几乎没有bug。如果你考虑并调试你的代码,你会得到答案。
- 您的API控制器名称
和MVC控制器名称EmployeeController
相同EmployeeController
- 您需要提供员工API的正确URL。在您的情况下,它可能位于某个文件夹中,因为在一个控制器文件夹中,您无法创建两个具有相同名称的控制器(在您的情况下,您的MVC和Web API控制器名称似乎相同)
- 使用DHC或Postmen检查WebAPI方法,检查您得到的响应,并在MVC控制器中使用相同的URL
代码中很少有bug。如果考虑和调试代码,你将得到答案。
- 您的API控制器名称
和MVC控制器名称EmployeeController
相同EmployeeController
- 您需要提供员工API的正确URL。在您的情况下,它可能位于某个文件夹中,因为在一个控制器文件夹中,您无法创建两个具有相同名称的控制器(在您的情况下,您的MVC和Web API控制器名称似乎相同)
- 使用DHC或Postmen检查WebAPI方法,检查您得到的响应,并在MVC控制器中使用相同的URL
ServiceAccess
的代码?添加了部分service Accessss class你是如何调用这个api的?我补充道,请再看一看。你的问题真的很混乱;你的一半代码甚至与你的问题无关。如果api工作并返回JSON,可能与ServiceAccess类有关。你的问题不清楚。你能展示ServiceAccess
的代码吗?添加了服务a的一部分成功班
public static HttpClient httpClient;
static ServiceAccess()
{
httpClient = new HttpClient();
httpClient.BaseAddress = GeneralConstants.ServiceBaseURI;
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<T> DeserializeAs<T>(HttpResponseMessage SerializedResponse)
{
return JsonConvert.DeserializeObject<T>(await SerializedResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
}
//mis named the name should be GetMany
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static async Task<HttpResponseMessage> GetAll(string uri)
{
HttpResponseMessage response = await httpClient.GetAsync(uri).ConfigureAwait(false);
return response;
}
<add key="ServiceBaseURI" value="http://localhost:60783/api/" />
http://localhost:60783/Employee/EmployeeList/