WebAPI 自定义路由给出 404

WebAPI custom route gives 404

在这个 webAPI 控制器中,Action PSWS return 404。它之前工作正常,但现在显示 404。代码没有任何变化,只是增加了几个控制器,这与不同的实体。调试器只是不捕获任何东西或在断点处停止,我已经尝试了所有知识或资源来解释它为什么显示 404 ,但没有任何效果。

我正在通过关注 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

任何帮助或指示将不胜感激:)

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");
        }
    }
}

您的方法和 URL 在语法上是正确的。 StartFrom 和 Expires 的 DateTime 参数的值可能是错误的,这会导致 404。

DateTime 解析将取决于系统配置 dd/mm/yyyy 与 mm/dd/yyyy 等等。

要验证,请将 Url 更改为有效范围内月份和日期可互换的日期(例如 1-1-2017),或者更好地将参数类型更改为 string 并根据您的预期控制代码中的解析。 DateTime.Parse("dd/MM/yyyy")

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.

话虽如此,除非参数 {start:DateTime}/Expires/{end:DateTime} 是拼写错误,否则根据它们应为 {start:datetime}/Expires/{end:datetime}.

的情况,对这些参数的约束可能不正确

接下来,示例 URL 具有无效的不变区域性日期。

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.

也就是说,基于约束的预期日期应遵循以下固定区域性格式,例如 yyyy-MM-ddyyyy-MM-dd h:mmtt

这意味着

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

实际上应该是

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