Gettting未找到与请求URI

时间:2016-07-27 18:38:56

标签: c# asp.net-web-api asp.net-web-api2 asp.net-web-api-routing

因此,当我尝试从其他网站点击我的网络密码时,我遇到以下错误。

这里是返回的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 }
            );
        }

1 个答案:

答案 0 :(得分:0)

好的,我找到了这个问题。
我得到了一些混乱的错误消息。

上面的错误是修复的,因为调用web.config的参数的编码问题。我在值> _<

中有&而不是&