简体   繁体   中英

What's wrong with my routes

I have the following routes declared in the webApiConfig .

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

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

Here are the route attributes on the controller:

[Route("users")]
public IHttpActionResult Get()


[Route("users/{id}")]
public IHttpActionResult Get(int id)

[AllowAnonymous]
[HttpPost]
[Route("users/validate")]
public IHttpActionResult Validate(string email)

When I make a call to:
~/api/users -- it works
~/api/users/1 -- it works
~/api/users/validate -- fails... trys to go into the api/users/1 but fails because of the Post verb.

How do I set up the routes so that I can validate a user in the user controller?

Try the use attribute routing to decorate the validate method:

http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Eg

[Route("customers/{customerId}/orders")]

public IEnumerable GetOrdersByCustomer(int customerId) { ... }

使用PostmanFiddler测试POST操作,因为您无法使用浏览器执行该操作。

You do not need to register Custom Route , as you already have Attribute Route .

Remove DefaultApiWithAction route from your code. Instead, you will need RoutePrefix .

[RoutePrefix("api/users")]
public class UsersController : ApiController
{
    public IHttpActionResult Get()
    {
        return Ok(new [] {"value1", "value2"});
    }

    public IHttpActionResult Get(int id)
    {
        return Ok(id);
    }

    [HttpPost]
    [Route("validate")]
    public IHttpActionResult Validate([FromBody]string email)
    {
        return Ok(email);
    }
}

How to Test HttpPost

Install Postman in Chrome browser. You can also use Fiddler or some other tools.

  1. Select POST and enter URL http://localhost:XXXXX/api/users/validate
  2. Select application/json as content type
  3. Enter content "johndoe@example.com"
  4. After clicking Send, you should see the result

在此处输入图片说明

API Break Point

在此处输入图片说明

You have not enabled Attribute Routing . You have omitted the MapHttpAttributeRoutes on the HttpConfiguration .

public static void Register(HttpConfiguration config)
{
    // Attribute routing.
    config.MapHttpAttributeRoutes();

    // Convention-based routing.
    config.Routes.MapHttpRoute(
       name: "DefaultApiWithAction",
       routeTemplate: "api/{controller}/{action}/{id}",
       defaults: new { id = RouteParameter.Optional }
    );

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

You also need to make sure you setup the routes properly on the ApiController as well

[RoutePrefix("api/users")]
public class UsersController : ApiController {

    //GET api/users
    [HttpGet]
    [Route("")]
    public IHttpActionResult Get() { ... }

    //GET api/users/5    
    [HttpGet]    
    [Route("{id}")]
    public IHttpActionResult Get(int id) { ... }

    //POST api/users/validate?email=someone@email.com
    [AllowAnonymous]
    [HttpPost]
    [Route("validate")]
    public IHttpActionResult Validate(string email) { ... }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM