自托管webapi中的路由问题

时间:2017-11-21 04:00:59

标签: c# model-view-controller asp.net-mvc-routing self-host-webapi

在自托管的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();

        }
    }

我错过了什么?

3 个答案:

答案 0 :(得分:3)

更改路线

[HTTPGET,路线(" API / {ID} /流/配置&#34)]

答案 1 :(得分:1)

通过此类路由,http://localhost:9998/shri/flows/config网址可​​以访问您的操作(不包含/api部分)

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

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

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

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

答案 2 :(得分: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] 或 {,它也不会从地址本身获取参数值{1}}。

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