Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在web API MVC 4中找不到资源错误_C#_.net_Asp.net Mvc 4_Asp.net Web Api_Restful Architecture - Fatal编程技术网

C# 在web API MVC 4中找不到资源错误

C# 在web API MVC 4中找不到资源错误,c#,.net,asp.net-mvc-4,asp.net-web-api,restful-architecture,C#,.net,Asp.net Mvc 4,Asp.net Web Api,Restful Architecture,我的web api看起来工作正常,它以json格式显示数据 http://localhost:60783/api/employee/GetEmployeeList/ 但当我试图指出.cshtm文件时,它会给我: The resource cannot be found error . 我的routeConfig看起来像这样 routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(

我的web 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 }
        );
my Global.asax:

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);           
        BundleConfig.RegisterBundles(BundleTable.Bundles);
我有一个名为
EmployeeController
我正在使用

 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();
    }

感谢您的帮助。

您的代码中几乎没有bug。如果你考虑并调试你的代码,你会得到答案。
  • 您的API控制器名称
    EmployeeController
    和MVC控制器名称
    EmployeeController
    相同
  • 您需要提供员工API的正确URL。在您的情况下,它可能位于某个文件夹中,因为在一个控制器文件夹中,您无法创建两个具有相同名称的控制器(在您的情况下,您的MVC和Web API控制器名称似乎相同)
  • 使用DHC或Postmen检查WebAPI方法,检查您得到的响应,并在MVC控制器中使用相同的URL

代码中很少有bug。如果考虑和调试代码,你将得到答案。

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

您是如何调用此api的?我添加了,请再看一看。您的问题真的很混乱;您的一半代码甚至与您的问题无关。如果api工作并返回JSON,则可能与ServiceAccess类有关。您的问题不清楚。您能否显示
ServiceAccess
的代码?添加了部分service Accessss class你是如何调用这个api的?我补充道,请再看一看。你的问题真的很混乱;你的一半代码甚至与你的问题无关。如果api工作并返回JSON,可能与ServiceAccess类有关。你的问题不清楚。你能展示
ServiceAccess
的代码吗?添加了服务a的一部分成功班
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;
    }
<add key="ServiceBaseURI" value="http://localhost:60783/api/" />
http://localhost:60783/Employee/EmployeeList/