2

I am coding a web api in c# and I have a question in regards to the correct route to access a function called test.

Here is the class definition:

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

I have a RoutePrefix as follows:

[Route("test")]

Here is the function called test:

[Route("test")]
[System.Web.Http.AcceptVerbs("GET")]
[System.Web.Http.HttpGet]
public String test()
{
    return "test";
}

I am trying to access the following url: http://localhost/api/test

The above url is displaying the following exception:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /api/test

How can I access the test function, such that the string "test" is displayed in my browser?

EDIT

I have deployed to my local IIS, and the database connection strings are working correctly.

The address for the local IIS is http://localhost/

These are the urls that I have tried:

http://localhost/test

http://localhost/api/test

http://localhost/api/test/test

http://localhost/ItemsWebApiController/test

http://localhost/ItemsWebApi/test

All of the above return the error page.

Thanks

2
  • Do you have a Route prefix in your class as [Route("api")] as well? Otherwise I am not sure you access using "/api" in it. Commented Jan 25, 2016 at 8:08
  • It would be something like http://address/api/ItemsWebApi/test. Commented Jan 25, 2016 at 8:10

4 Answers 4

4

If you a putting [Route("test")] your url will be http://address/test

if you need url like http://address/api/test, change your route like [Route("api/test")]

Note: you need to add [HttpGet] as well

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

Comments

1

You have to activate Attribute Routing in WebAPI Controllers

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{id}",
           defaults: new { id = RouteParameter.Optional }
        );
    }
}

and in your application start

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

And then your URL is http://localhost/test, because the default route would not match here.

1 Comment

Thank you. The problem was that the GlobalConfiguration.Configure(WebApiConfig.Register); was not at the top of the function.
1

The route based on your configuration will be: http://address/api/test/test the first test is the route prefix from the attribute [Route("test")]; the scond test is the action in the controller from the attribute defined on the method

[Route("test")]
public String test()
{

Comments

1

Web api infers the http method based on your action name. It won't know from "Test" what to use.

See the docs

HTTP Methods

Web API also selects actions based on the HTTP method of the request (GET, POST, etc). By default, Web API looks for a case-insensitive match with the start of the controller method name. For example, a controller method named PutCustomers matches an HTTP PUT request.

You can override this convention by decorating the mathod with any the following attributes:

  • [HttpDelete]
  • [HttpGet]
  • [HttpHead]
  • [HttpOptions]
  • [HttpPatch]
  • [HttpPost]
  • [HttpPut]

The following example maps the CreateBook method to HTTP POST requests.

[Route("api/books")]
[HttpPost]
public HttpResponseMessage CreateBook(Book book) { ... }

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.