Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 在c中调用web api#_C#_Asp.net_Asp.net Mvc 4_Asp.net Web Api - Fatal编程技术网

C# 在c中调用web api#

C# 在c中调用web api#,c#,asp.net,asp.net-mvc-4,asp.net-web-api,C#,Asp.net,Asp.net Mvc 4,Asp.net Web Api,我试图在本地调用我的webapi。这是我的邮递员网址,它工作得很好 当从MVC应用程序调用时,我得到一个异常404未找到 这是我的学生管理员 var url = "/Students"; string test = ApiHelper.ApiClient.BaseAddress + url; using (HttpResponseMessage response = await ApiHelper.ApiClient.Get

我试图在本地调用我的webapi。这是我的邮递员网址,它工作得很好

当从MVC应用程序调用时,我得到一个异常404未找到

这是我的学生管理员

            var url = "/Students";
            string test = ApiHelper.ApiClient.BaseAddress + url;
            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    listStudent = await response.Content.ReadAsAsync<List<StudentModel>>();
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
当我调试它时,我发现在我的响应中,请求URI

RequestUri  {http://localhost:8080/Student}
这就是调用我的api位置的地方

  <appSettings>
    <add key="ApiUrl" value="http://localhost:8080/api/V1" />
  </appSettings>


我在尝试调用本地api时做错了什么?

api/v1是路由前缀。再次使用此路由前缀和托盘装饰BaseAddress控制器。像这样:

[RoutePrefix("api/V1")]  
    public class ProductController : Controller  
    { 

试着用
api/V1/Students
调用
GetAsync(url)
,而不是
/Students
这样做了,但是为什么我要把api/V1放在我所有的web api调用中呢?这听起来太过分了。我不知道你是如何定义api的路由的,但我试着只添加
[route(“api/V1/[controller]”路由)
,然后我用
GetAsync调用它(“/Students”)
。这很好。不,我想说的是,如果我使用GetASync(api/V1/Students),它是可以工作的,但是为每个控制器/方法声明[route(…)]似乎很乏味,要在每个方法/控制器上编写它。如果我想从V1更改为v2,那么我需要将所有方法从route(api/V1…更改为route(api/v2…)这就是为什么我创建了apihelper,这样我就可以在一个中心位置拥有我的基于元素。你的解决方案但是在每个控制器上。我将来可能有上千个控制器。但是你的解决方案确实有效。在这种情况下,通过测试更改GetAsync方法的url。并且将对
[RoutePrefix("api/V1")]  
    public class ProductController : Controller  
    {