1

在自托管 webapi 控制台应用程序项目中,我无法SayHello使用http://localhost:9998/api/shri/flows/config.

错误:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:9998/api/shri/flows/config'.",
    "MessageDetail": "No route data was found for this request."
}

控制器:

    class ConfigController : ApiController
    {
        [HttpGet, Route("{id}/flows/config")]
        public string SayHello([FromUri] string id)
        {
            return "Hello " + id;
        }
    }

启动:

    public class Startup
    {
        // This code configures Web API. The Startup class is specified as a type
        // parameter in the WebApp.Start method.
        public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            appBuilder.UseWebApi(config);
        }
    }

为了保持自托管 webapi 运行,我有以下内容:

    public class Program
    {
        static void Main()
        {
            Uri myUri = new Uri(@"http://localhost:9998/");
            // Let have our configurations readY
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(myUri);

            // configure routes
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            HttpSelfHostServer server = new HttpSelfHostServer(config);

            // Start listening 
            server.OpenAsync().Wait();

            Console.WriteLine("WebApi hosted on " + myUri.AbsoluteUri + " It can be tested now");
            Console.ReadLine();

        }
    }

我错过了什么?

4

3 回答 3

4

更改路线

[HttpGet,Route("api/{id}/flows/config")]

于 2017-11-21T04:18:48.797 回答
2

通过这样的路由,您的操作将可以通过http://localhost:9998/shri/flows/configurl 访问(不包括/api部分)

如果您想使用http://localhost:9998/api/shri/flows/configurl 访问该操作,请使用该操作的正确Route属性:

[HttpGet, Route("api/{id}/flows/config")]
public string SayHello([FromUri] string id)

或在控制器类上添加RoutePrefix属性:

[RoutePrefix("api")]
public class ConfigController : ApiController
于 2017-11-21T04:19:10.640 回答
0

对于将来可能会为此苦苦挣扎的人,正如 Nkosi 所说,您需要启用属性路由。

 public class Program
    {
        static void Main()
        {
            Uri myUri = new Uri(@"http://localhost:9998/");
            // Let have our configurations readY
            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(myUri);

            // enables the attribute routing
            config.MapHttpAttributeRoutes();

            // configure routes
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            HttpSelfHostServer server = new HttpSelfHostServer(config);

            // Start listening 
            server.OpenAsync().Wait();

            Console.WriteLine("WebApi hosted on " + myUri.AbsoluteUri + " It can be 
            tested now");
            Console.ReadLine();

        }
    }

如果未配置属性路由,那么您的路由是否正确无关紧要,当向 API 发出请求时,即使您使用[FromBody]or ,它也不会从地址本身获取参数值[FromUri]

我希望这对某人有所帮助,这是我的问题。

于 2021-07-07T08:51:27.387 回答