繁体   English   中英

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

[英]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/

任何帮助表示赞赏。

您的代码中几乎没有错误。 如果您考虑并调试代码,您将得到答案。

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

暂无
暂无

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

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