简体   繁体   中英

Web Api attribute route not working

I have a web api using the owin pipeline. I am only using attribute routing. I have 3 controllers with a few routes on each. All the routes work sans one.

ContactsController

[RoutePrefix("accounts/{accountNumber}/contacts")]
public class ContactsController : ApiController
{
    [HttpGet]
    [Route("")]
    [Route("{contactId}")]
    public async Task<IHttpActionResult> GetContactsAsync(
        string accountNumber, 
        int? contactId = null);

    [HttpPut]
    [Route("{contactId}")]
    public async Task<IHttpActionResult> UpdateContactsAsync(
        [FromBody] UpdateContactsRequest request,
        string accountNumber,
        int contactId)
}

GET: http://localhost/accounts/1/contacts -- Works
GET: http://localhost/accounts/1/contacts/1 -- Works
PUT: http://localhost/accounts/1/contacts/1 -- DOESN'T WORK

I get a 404 on the PUT. It doesn't find the route.

I've also tried the following routes on my PUT:

[Route("/accounts/{accountNumber}/contacts/{contactId}")]
[Route("~/accounts/{accountNumber}/contacts/{contactId}")]

And I've tried rearranging the parameters in various ways as well.

UPDATE 1: Postman call 邮差

UPDATE 2: Request object

public class UpdateContactsRequest
{
    public int Id { get; set; }
    public Address Address { get; set; }
    public string PrimaryPhoneNumber { get; set; }
    public string AlternatePhoneNumber { get; set; }
    public string FaxNumber { get; set; }
    public string EmailAddress { get; set; }
}

public class Address
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
}

The issue ended up being a web.config configuration issue.

I was missing a system.webServer config section. I added that section with the following copied from another web.config:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <remove name="OPTIONSVerbHandler" />
        <remove name="TRACEVerbHandler" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

Now all is well, and it finds my PUT route.

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