未找到与请求 URI 匹配的 HTTP 资源
Getting No HTTP resource was found that matches the request URI
我在尝试从另一个站点访问我的 webapi 代码时遇到以下错误。
这是返回的 json 消息。
"Message":"No HTTP resource was found that matches the request URI
'http://localhost:47845/api/Test?samAccountName=aasdf&success=True&permissionName=bla'.","MessageDetail":
"No action was found on the controller 'Test' that matches the request."
我得到了一个不错的成功字符串。
当我用下面的代码点击它时,我得到上面的错误信息。
using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var values = HttpUtility.UrlDecode(string.Format(
CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["PathToParameters"].ToString(),
"someAccount".ToUpper(CultureInfo.InvariantCulture),
true.ToString(),
"MyPermission".ToUpper(CultureInfo.InvariantCulture)));
lblResult.Text = string.Empty;
var newUrl = new Uri(ConfigurationManager.AppSettings["Url"].ToString() + values);
// HTTP GET
var response = await client.GetAsync(newUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
//var response = await client.GetAsync(values, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
this.lblResult.Text = await response.Content.ReadAsStringAsync();
}
这是我的 api 控制器
public class TestController : ApiController
{
// GET: api/Test
public string Get(string samAccountName, bool success, string permissionName)
{
var returnValue = "Success";
return returnValue;
}
}
这是我的路线
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
好的,我想通了这个问题。
我一整天都收到了一些错误消息。
上述错误已修复,因为 web.config 调用的参数编码存在问题。我在值 >_<
中有 &
而不是 &
我在尝试从另一个站点访问我的 webapi 代码时遇到以下错误。
这是返回的 json 消息。
"Message":"No HTTP resource was found that matches the request URI
'http://localhost:47845/api/Test?samAccountName=aasdf&success=True&permissionName=bla'.","MessageDetail":
"No action was found on the controller 'Test' that matches the request."
我得到了一个不错的成功字符串。
当我用下面的代码点击它时,我得到上面的错误信息。
using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var values = HttpUtility.UrlDecode(string.Format(
CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["PathToParameters"].ToString(),
"someAccount".ToUpper(CultureInfo.InvariantCulture),
true.ToString(),
"MyPermission".ToUpper(CultureInfo.InvariantCulture)));
lblResult.Text = string.Empty;
var newUrl = new Uri(ConfigurationManager.AppSettings["Url"].ToString() + values);
// HTTP GET
var response = await client.GetAsync(newUrl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
//var response = await client.GetAsync(values, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
this.lblResult.Text = await response.Content.ReadAsStringAsync();
}
这是我的 api 控制器
public class TestController : ApiController
{
// GET: api/Test
public string Get(string samAccountName, bool success, string permissionName)
{
var returnValue = "Success";
return returnValue;
}
}
这是我的路线
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
好的,我想通了这个问题。
我一整天都收到了一些错误消息。
上述错误已修复,因为 web.config 调用的参数编码存在问题。我在值 >_<
中有&
而不是 &