0

I'm accessing a WebAPI resource like this:

localhost/myapp/api/values?ParamOne=123&ParamTwo=testing

In ValuesController, I have this:

public class MyParams {
  public int? ParamOne {get;set;}
  public string ParamTwo {get;set;}
}

[HttpGet]
public HttpResponseMessage Get([FromUri]MyParams someparams) {
  ...
}

When I try to access the resource, I get this error:

HTTP Error 403.14 Forbidden The Web server is configured to not list the contents of this directory

Here's the RouteConfig.cs, which is just the default:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
  name: "Default",
  url: "{controller}/{action}/{id}",
  defaults: new {controller="Home", action="Index", id=UrlParameter.Optional}

Anyone know what I'm doing wrong?

8
  • Post the route config. Commented Sep 16, 2016 at 17:22
  • Just posted. I haven't changed the RouteConfig. Commented Sep 16, 2016 at 17:26
  • Is it an api controller or mvc controller? The route config looks like mvc config while the uri looks like api. Commented Sep 16, 2016 at 17:43
  • It should be api. This is a WebAPI app. Commented Sep 16, 2016 at 17:43
  • Look for the web api config inside the WebApiConfig file inside App_Start folder. Commented Sep 16, 2016 at 17:45

1 Answer 1

1

Your WebAPI Get endpoint is expecting someparams as parameter not ParamOne and ParamTwo.

Changing your endpoint signature to the below should work with the given URL:

localhost/myapp/api/values?ParamOne=123&ParamTwo=testing

public HttpResponseMessage Get(int? ParamOne, string ParamTwo)

Updates

The route configuration above in your question is for MVC controller not WebAPI Controller. See below for WebAPI route config:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

In Global.asax.cs Application_Start() method, it is registered like so:

GlobalConfiguration.Configure(WebApiConfig.Register);

NuGet packages required:

  • Microsoft.AspNet.WebApi.Core
  • Microsoft.AspNet.WebApi.WebHost

One more thing, your controller must inherit from ApiController not Controller

Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't work either. What does the RouteConfig look like for this?

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.