自托管 webapi 中的路由问题

Routing issue in self hosted webapi

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

错误:

{
    "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();

        }
    }

我错过了什么?

更改路线

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

通过这样的路由,http://localhost:9998/shri/flows/config url 可以访问您的操作(没有 /api 部分)

如果您想使用 http://localhost:9998/api/shri/flows/config url 访问该操作,要么更正该操作的 Route 属性:

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

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

[RoutePrefix("api")]
public class ConfigController : ApiController

对于将来可能会为此苦苦挣扎的人,正如 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][FromUri].

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