简体   繁体   中英

WebAPI custom route gives 404

In this webAPI controller, Action PSWS return 404. It was working fine earlier but now its shows 404. Nothing is changed in code other than having few more controllers, which are related to different entities. Debugger just don't catch anything or stops at breakpoints, and I have tried everything in knowledge or resources to why it show 404 , but nothing works.

I am accessing this action through following url

http://localhost:43827/api/Pss/PSWS/cs/b3dc86d8-9b55-4c7e-95f3-7bab0908510d/ps/1eaaaa5e-dd32-4d29-9b85-16c1eb7890f4/pss/6af508be-c2ca-4d00-aeb4-dd87cabf72d4/quantity/4/SerialPrefix/FP_P/StartFrom/21-11-2017/Expires/21-11-2019

Any help or pointer would be highly appreciated :)

namespace API.Controllers
{
    [RoutePrefix("api/Pss")]
    public class myEntityController : ApiController
    {
        private localEntities db = new localEntities();

        [HttpGet]
        [Route("")]
        // GET: api/myEntity
         public async Task<IHttpActionResult> GetmyEntity(){ return OK("OK")}

        [HttpGet]
        [Route("{keys:guid}")]
        // GET: api/myEntity/1f7dc74f-af14-428d-aa31-147628e965b2
        [ResponseType(typeof(myEntity))]
         public async Task<IHttpActionResult> GetmyEntity(Guid keys){ return OK("OK")}

        // PUT: api/myEntity/1f7dc74f-af14-428d-aa31-147628e965b2
        [HttpPut]
        [Route("{keys:guid}", Name = "PutmyEntity")]
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> PutmyEntity(Guid keys){ return OK("OK")}

        // POST: api/myEntity
        [HttpPost]
        [Route("", Name = "PostmyEntity")]
        [ResponseType(typeof(myEntity))]
        public async Task<IHttpActionResult> PostmyEntity(Guid keys,myEntity entity){ return OK("OK")}

        [HttpGet]
        [Route("psws/cs/{cpKeys:guid}/ps/{prkKeys:guid}/pss/{prkSKeys:guid}/quantity/{quantity:int}/SerialPrefix/{_SerialPrefix:length(3,20)}/StartFrom/{start:DateTime}/Expires/{end:DateTime}"] 
        [ResponseType(typeof(JObject))]
        public IHttpActionResult PSWS(Guid cpKeys, Guid prkKeys, Guid prkSKeys, int quantity, string _SerialPrefix, DateTime start, DateTime end)         
        {
            return Ok("kill");
        }
    }
}

Referencing the following documentation at Route Constraint Reference

Warning

Avoid using constraints for input validation, because doing so means that invalid input will result in a 404 (Not Found) instead of a 400 with an appropriate error message. Route constraints should be used to disambiguate between similar routes, not to validate the inputs for a particular route.

with that said, and unless the parameters {start:DateTime}/Expires/{end:DateTime} is a typo, then the constraint on those parameters may be incorrect based on their case where they should be {start:datetime}/Expires/{end:datetime} .

Next, the example URL has invalid invariant culture dates.

Warning

Route constraints that verify the URL can be converted to a CLR type (such as int or DateTime ) always use the invariant culture - they assume the URL is non-localizable. The framework-provided route constraints do not modify the values stored in route values. All route values parsed from the URL will be stored as strings. For example, the Float route constraint will attempt to convert the route value to a float, but the converted value is used only to verify it can be converted to a float.

That said, the expected date based on constraint should follow the following an invariant culture format like yyyy-MM-dd or yyyy-MM-dd h:mmtt .

Which means

...StartFrom/21-11-2017/Expires/21-11-2019

Should actually be

...StartFrom/2017-11-21/Expires/2019-11-21

Your method and URL are syntactically right. The values for your DateTime parameters for StartFrom and Expires are probably wrong which cause the 404.

The DateTime parsing will depend on the system configuration dd/mm/yyyy vs. mm/dd/yyyy and so on.

To validate, change your Url to a date where the month and day in a valid range interchangeably (eg 1-1-2017) or even better change the type of the parameter to string and control the parsing in code based on how you expect it to be. DateTime.Parse("dd/MM/yyyy")

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