简体   繁体   English

WebAPI自定义路由给出404

[英]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. 在此webAPI控制器中,Action PSWS返回404。它早先运行良好,但现在显示404。除了很少有与不同实体相关的控制器以外,代码没有任何改变。 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. 调试器什么也没有捕获,也没有在断点处停止,我已经尝试了所有知识或资源,尝试为什么它显示404,但是没有任何效果。

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. 避免使用约束进行输入验证,因为这样做意味着无效输入将导致404(未找到),而不是带有适当错误消息的400 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} . 话虽如此,并且除非参数{start:DateTime}/Expires/{end:DateTime}是一个错字,否则基于这些参数的约束条件可能会不正确,具体取决于它们应为{start:datetime}/Expires/{end:datetime}

Next, the example URL has invalid invariant culture dates. 接下来,示例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. 验证URL可以转换为CLR类型(例如intDateTime )的路由约束始终使用不变的区域性 -它们假定URL不可本地化。 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. 从URL解析的所有路由值都将存储为字符串。 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 . 也就是说,基于约束的预期日期应遵循以下不变的文化格式,例如yyyy-MM-ddyyyy-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. 您的方法和URL在语法上是正确的。 The values for your DateTime parameters for StartFrom and Expires are probably wrong which cause the 404. StartFrom和Expires的DateTime参数的值可能错误,导致404。

The DateTime parsing will depend on the system configuration dd/mm/yyyy vs. mm/dd/yyyy and so on. DateTime解析将取决于系统配置dd / mm / yyyy与mm / dd / yyyy等。

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. 为了进行验证,请将您的网址更改为有效范围内的月份和日期可以互换的日期(例如1-1-2017),或者甚至更好地将参数的类型更改为字符串,并根据您的期望控制代码中的解析成为。 DateTime.Parse("dd/MM/yyyy") DateTime.Parse( “DD / MM / YYYY”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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