My web api looks working and it dispalys data in json format
http://localhost:60783/api/employee/GetEmployeeList/
but when i try to point out the .cshtm file then it gives me :
The resource cannot be found error .
my routeConfig looks like this
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And my 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);
I have controller called EmployeeController
i amcalling the api using
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();
}
service Access class :
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;
}
and web config file has this
<add key="ServiceBaseURI" value="http://localhost:60783/api/" />
the lnk which gives me problem :
http://localhost:60783/Employee/EmployeeList/
Any help appreciated.
There are few bug in your code. If you consider and debug your code you will get the answer.
EmployeeController
and MVC controller name EmployeeController
is same.