3
因此,在尝试从其他站点打我的webapi代码时出现以下错误。Gettting未找到与请求URI相匹配的HTTP资源
这是返回的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 }
);
}
您的'RouteConfig'不应该为'TestController'具有'MapRoute'吗?你上面显示的仅仅是'HomeController'。 – Shiva
显示Web API的路由配置。它通常在'WebApiConfig.cs' – Nkosi
我已经添加了webApiConfig.cs值(它只是默认值)。我还添加了更多的信息,因为在开发中这工作正常,但它只是在我们的生产环境中,它不能正常工作。 – Lareau