[英]The resource cannot be found error in web API MVC 4
我的网络API看起来可以正常工作,并且以JSON格式显示数据
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 }
);
我的Global.asax:
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
我有一个名为EmployeeController
控制器,我正在使用api
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();
}
服务访问类别:
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;
}
和Web配置文件有这个
<add key="ServiceBaseURI" value="http://localhost:60783/api/" />
给我问题的lnk:
http://localhost:60783/Employee/EmployeeList/
任何帮助表示赞赏。
您的代码中几乎没有错误。 如果您考虑并调试代码,您将得到答案。
EmployeeController
和MVC控制器名称EmployeeController
是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.