1

I might have misunderstood the concept of MapHttpRoute entirely. I'm developing an asp.net web api, having hard time getting the Routing to work.

I have a MyUserController.cs and MyTaskController.cs. MyUserController accepts username/password and returns a sessionkey, it works as expected.

But MyTaskController keeps complaining when I pass the second parameter. http://localhost:59720/api/mytask/somesessionkey - works

http://localhost:59720/api/mytask/somesessionkey/1 - doesn't work, throws the following error

{"Message":"The requested resource does not support http method 'GET'."}

I tried with and without [HttpGet] on the methods.

MyTaskController.cs

//works fine
public IEnumerable<MyTask> Get(string sessionkey)
{
    MyTaskModel mtm = new MyTaskModel();
    return mtm.GetAll();
}

//doesn't work
public string Get(string sessionkey, int id)
{
    if(isValid(sessionkey))
    {
        return DataAccess().GetTask(id);
    }
    return "";
}

MyUserController.cs

public string Get(string username, string password)
{
    string sessionkey = "tempsession";
    return sessionkey;
}

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services
    // Configure Web API to use only bearer token authentication.
    config.SuppressDefaultHostAuthentication();
    config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "MyUserApi",
        routeTemplate: "api/{controller}/{username}/{password}",
        defaults: new { controller = "MyUser" }
    );

    config.Routes.MapHttpRoute(
        name: "MyTaskApi",
        routeTemplate: "api/Mytask/{sessionkey}/{id}",
        defaults: new
        {
            controller = "MyTask",
            id= RouteParameter.Optional
        }
    );

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

    config.Formatters.Remove(config.Formatters.XmlFormatter);
    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
}

3 Answers 3

1

I got it to work by using the controller name "MyUser" instead of "{controller}". I thought it would use the default value specified as controller="MyUser", but I have no idea why it wouldn't.

Non working code:

config.Routes.MapHttpRoute(
        name: "MyUserApi",
        routeTemplate: "api/{controller}/{username}/{password}",
        defaults: new { controller = "MyUser" }
    );

Working code:

config.Routes.MapHttpRoute(
        name: "MyUserApi",
        routeTemplate: "api/MyUser/{username}/{password}",
        defaults: new { controller = "MyUser" }
    );
Sign up to request clarification or add additional context in comments.

Comments

0

try to change the order in that you define the routes, first put the route related with mytaskcontroller:

config.Routes.MapHttpRoute(
        name: "MyTaskApi",
        routeTemplate: "api/Mytask/{sessionkey}/{id}",
        defaults: new
        {
            controller = "MyTask",
            id= RouteParameter.Optional
        }
    );

config.Routes.MapHttpRoute(
        name: "MyUserApi",
        routeTemplate: "api/{controller}/{username}/{password}",
        defaults: new { controller = "MyUser" }
    );

Comments

0

you can give it a try with webapi 2 attributes routing.

[Route("GetByID/{SessionID}/{Id}")]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.