AttributeRouting icon indicating copy to clipboard operation
AttributeRouting copied to clipboard

Optional parameters are not working with selfhost web api application

Open nbcwasp opened this issue 12 years ago • 4 comments

I am trying to create a selfhost web api application using attributerouting and it seems like optional parameters are not working. Or maybe I do it wrong.

Here is a sample code:

using System; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Routing; using System.Web.Http.SelfHost;

using AttributeRouting; using AttributeRouting.Web.Http; using AttributeRouting.Web.Http.SelfHost;

namespace attributeroutingtest { [RoutePrefix("api/test")] public class TestController : ApiController { [GET("mymeth/{param1?}"), HttpGet] public HttpResponseMessage MyMeth(string param1) { return Request.CreateResponse(HttpStatusCode.OK); } }

class Program
{
    static void Main()
    {
        try
        {

            var config = new HttpSelfHostConfiguration("http://localhost:333");
            config.Routes.MapHttpAttributeRoutes();
            foreach (HttpRoute httpRoute in config.Routes.Cast<HttpRoute>())
            {
                Console.WriteLine(httpRoute.RouteTemplate);
            }
            using (var server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
                Console.WriteLine("Press Enter to quit.");
                Console.ReadLine();
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex);
            Console.ReadLine();
        }
    }
}

}

When I use address http://localhost:333/api/test/mymeth/somevalue I get 200 (OK), if I use http://localhost:333/api/test/mymeth I get a 404 which is not what I expected.

nbcwasp avatar Jul 03 '13 14:07 nbcwasp

Optional query params do not work in web api due to integration issues.

On Jul 3, 2013, at 8:59 AM, nbcwasp [email protected] wrote:

I am trying to create a selfhost web api application using attributerouting and it seems like optional parameters are not working. Or maybe I do it wrong.

Here is a sample code:

using System; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Routing; using System.Web.Http.SelfHost;

using AttributeRouting; using AttributeRouting.Web.Http; using AttributeRouting.Web.Http.SelfHost;

namespace attributeroutingtest { [RoutePrefix("api/test")] public class TestController : ApiController { [GET("mymeth/{param1?}"), HttpGet] public HttpResponseMessage MyMeth(string param1) { return Request.CreateResponse(HttpStatusCode.OK); } }

class Program { static void Main() { try {

        var config = new HttpSelfHostConfiguration("http://localhost:333");
        config.Routes.MapHttpAttributeRoutes();
        foreach (HttpRoute httpRoute in config.Routes.Cast<HttpRoute>())
        {
            Console.WriteLine(httpRoute.RouteTemplate);
        }
        using (var server = new HttpSelfHostServer(config))
        {
            server.OpenAsync().Wait();
            Console.WriteLine("Press Enter to quit.");
            Console.ReadLine();
        }
    }
    catch (Exception ex)
    {
        Console.Write(ex);
        Console.ReadLine();
    }
}

} }

When I use address http://localhost:333/api/test/mymeth/somevalue I get 200 (OK), if I use http://localhost:333/api/test/mymeth I get a 404 which is not what I expected.

— Reply to this email directly or view it on GitHub.

mccalltd avatar Jul 03 '13 15:07 mccalltd

Would you be so kind then to put this in documentation somewhere? Or it is already there and I've missed it?

nbcwasp avatar Jul 03 '13 15:07 nbcwasp

I believe it's on the web site and marked in red already. :) http://attributerouting.net/#asp-net-web-api

On Jul 3, 2013, at 9:03 AM, nbcwasp [email protected] wrote:

Would you be so kind then to put this in documentation somewhere? Or it is already there and I've missed it?

— Reply to this email directly or view it on GitHub.

mccalltd avatar Jul 03 '13 15:07 mccalltd

Ooouch, I am so sorry I've missed it. Thank you for your patience and such quick response.

nbcwasp avatar Jul 03 '13 15:07 nbcwasp