locked
Defining a route for self hosting in .Net Standard RRS feed

  • Question

  • User360451555 posted

    Hi, i cant seem to figure out what should be the route defined to reach my post method in a self-hosting website. It's running .Net Standard and eventually it will run as a windows service. 

    Here is how i have tried to structure the routing table

    public static void Main(string[] args)
            {
    
                var config = new HttpSelfHostConfiguration(new Uri("http://localhost:43634"));
    
                config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional });
    
                InitPrintEngine();
    
                using (HttpSelfHostServer server = new HttpSelfHostServer(config))
                {
                    server.OpenAsync().Wait();
                    Console.WriteLine("Press Enter to quit.");
                    Console.ReadLine();
                }
            }
        [Route("[api/controller]")]
        public class LabelController : ApiController
        {
            [System.Web.Mvc.HttpGet]
            public IEnumerable<string> Get()
            {
                return new[] {"Label1", "Label2"};
            }
    
            [System.Web.Mvc.HttpPost, Route("label/labelvariables")]
            public async Task<HttpResponseMessage> PostFile(HttpPostedFile labelFile)
            {
    
             }
        }

    I've omitted the details of the PostFile method to have it as simple as possible. I cant reach the method though through postman. 

    This is how am trying to invoke the method

    http://localhost:43634/api/Label/labelvariables/labelFile

    Friday, May 17, 2019 10:32 PM

All replies

  • User1724605321 posted

    Hi feiyim ,

    Your route could be modified like :

        [RoutePrefix("api/Label")]
        public class LabelController : ApiController
        {
            [System.Web.Mvc.HttpGet]
            public IEnumerable<string> Get()
            {
                return new[] { "Label1", "Label2" };
            }
    
            [Route("labelvariables")]
            public async Task<string> PostValues()
            {
                return "";
            }
        }

    So that you can post to  "http://localhost:xxxxx/api/Label/labelvariables" .

    Please refer to below link for more details about Attribute Routing in ASP.NET Web API 2 :

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 

    Best Regards,

    Nan Yu

    Monday, May 20, 2019 3:24 AM