简体   繁体   English

Web API MVC 4中找不到资源错误

[英]The resource cannot be found error in web API MVC 4

My web api looks working and it dispalys data in json format 我的网络API看起来可以正常工作,并且以JSON格式显示数据

http://localhost:60783/api/employee/GetEmployeeList/

but when i try to point out the .cshtm file then it gives me : 但是当我尝试指出.cshtm文件时,它给了我:

 The resource cannot be found error . 

my routeConfig looks like this 我的routeConfig看起来像这样

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 : 还有我的webApiConfig:

 config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

my Global.asax : 我的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 我有一个名为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();
    }

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 和Web配置文件有这个

<add key="ServiceBaseURI" value="http://localhost:60783/api/" />

the lnk which gives me problem : 给我问题的lnk:

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. 您的API控制器名称EmployeeController和MVC控制器名称EmployeeController是相同的。
  • 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. 您需要提供Employee API的正确URL,因为它可能位于某个文件夹中,因为在一个控制器文件夹中您无法创建两个具有相同名称的控制器(在您的情况下,您的MVC和Web API控制器名称似乎是相同的。
  • 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. 使用DHC或邮递员来对Web API方法进行插拨,并检查您收到的响应,并在MVC控制器中使用相同的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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