C# Can';不要让Fiddler使用localhost不同的端口从MVC捕获HttpClient web api调用
然而,另一个小提琴手却没能把它抓住 与此类似,我现在花了两个小时阅读和尝试不同的解决方案,但没有一个允许我看到我的fiddler web api流量 顺便说一句,我的代码正在运行,我只是专注于让fiddler向我展示api调用 我将描述我的设置,然后我尝试了什么 我的Web API是一个独立的MVC 6、EF 7项目,运行在端口63381上 我的ASP.NET MVC 5 web客户端项目正在端口59722上运行 mvc客户端中的典型操作控制器:C# Can';不要让Fiddler使用localhost不同的端口从MVC捕获HttpClient web api调用,c#,asp.net,fiddler,C#,Asp.net,Fiddler,然而,另一个小提琴手却没能把它抓住 与此类似,我现在花了两个小时阅读和尝试不同的解决方案,但没有一个允许我看到我的fiddler web api流量 顺便说一句,我的代码正在运行,我只是专注于让fiddler向我展示api调用 我将描述我的设置,然后我尝试了什么 我的Web API是一个独立的MVC 6、EF 7项目,运行在端口63381上 我的ASP.NET MVC 5 web客户端项目正在端口59722上运行 mvc客户端中的典型操作控制器: //Controller CTOR publ
//Controller CTOR
public ClientController()
{
client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:63381/api/MyApi");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
//Action within ClientController
public async Task<JsonResult> AddNewSubCategory()
{
HttpResponseMessage responseMessage = await client.PostAsJsonAsync(url2, content);
if (responseMessage.IsSuccessStatusCode)
{
return Json("[{Update Success}]", JsonRequestBehavior.AllowGet);
}
return Json("[{Error Updating}]", JsonRequestBehavior.AllowGet);
}
}
//控制器
公共客户端控制器()
{
client=新的HttpClient();
client.BaseAddress=新Uri(“http://localhost:63381/api/MyApi");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
}
//ClientController中的操作
公共异步任务AddNewSubCategory()
{
HttpResponseMessage responseMessage=Wait client.PostAsJsonAsync(url2,内容);
if(responseMessage.IsSuccessStatusCode)
{
返回Json(“[{Update Success}]”,JsonRequestBehavior.AllowGet);
}
返回Json(“[{Error updatening}]”,JsonRequestBehavior.AllowGet);
}
}
更新:
我的机器被锁定,重新启动后,我开始看到api调用,所以这个问题与我的机器有关。很抱歉打扰您。问题很可能是您使用的是以特殊方式处理的localhost 尝试改用计算机名或ip(不要使用127.0.0.1) 文档中也有关于这方面的信息:
如果您试图在api中点击特定操作,请在webapi配置中使用该代码
public static void Register(HttpConfiguration config)
{
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional });
config.Routes.MapHttpRoute("API Default", "api/{controller}/{action}/{id}",
new { id = RouteParameter.Optional });
}
这段代码是您调用api的地方
public ActionResult ClientController(model content)
{
try
{
HttpClient client = new HttpClient("http://localhost:63381/");
client.BaseAddress = new Uri();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsJsonAsync("api/MyApi/url2", content).Result;
if (response.IsSuccessStatusCode)
{
return Json(new { code = 0, message = "Success" });
}
else
{
return Json(new { code = -1, message = "Failed" });
}
}
catch (Exception ex)
{
int code = -2;
return Json(new { code = code, message = "Failed" });
}
}
}
有在Fiddler中设置的过滤器吗?没有。抱歉,没有提到我签入了OP。我尝试了机器名称…请参阅OP中的第3点。现在可能是我没有正确执行。我将用我尝试过的方法更新OP。当我尝试从mvc客户端调用api时,HttpClient给我的主机名无效。请尝试改为使用您的ip。