簡體   English   中英

獲取沒有與請求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."

當我直接點擊網站時, 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 }
            );
        }

好的,我找到了這個問題。
我得到了一些混亂的錯誤消息。

上面的錯誤是固定的,因為調用web.config的參數的編碼問題。 我有&amp; 而不是&在值> _ <

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM