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." 

當我直接打到網站, http://MyServer/ApiToHit/api/Values?samAccountName=someAccount&success=True&permissionName=MyPermission

我得到一個不錯的成功字符串。

當我用下面的代碼命中它時,出現上面的錯誤信息。

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 } 
      ); 
     } 
+0

您的'RouteConfig'不應該爲'TestController'具有'MapRoute'嗎?你上面顯示的僅僅是'HomeController'。 – Shiva

+0

顯示Web API的路由配置。它通常在'WebApiConfig.cs' – Nkosi

+0

我已經添加了webApiConfig.cs值(它只是默認值)。我還添加了更多的信息,因爲在開發中這工作正常,但它只是在我們的生產環境中,它不能正常工作。 – Lareau

回答

0

好吧,這個我想通了這個問題與此有關。
我收到了一整天的錯誤消息。

上述錯誤已修復,因爲調用web.config的參數編碼問題。我有&amp;而不是&的值> _ <

相關問題