简体   繁体   中英

The resource cannot be found error in web API MVC 4

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.

  • Your API controller name EmployeeController and MVC controller name EmployeeController is same.
  • Your need to provide correct URL of the Employee API in your case it might be inside some folder because In one controller folder you can not create two controller with same name(in your case it seems like your MVC and Web API controller name is same.
  • Pock the web API method using DHC or Postmen and check what response you are getting and use the same URL in you MVC controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM