路由前缀命名限制?

时间:2016-03-01 21:43:07

标签: c# asp.net-web-api

我有一个简单的asp.net web api控制器和一个post方法。在那个api控制器的顶部,如果我用“ sigs ”命名RoutePrefix属性,那么后期操作正在运行;发布http://localhost:7178/sigs

  • 如果我使用“签名”等名称命名RoutePrefix属性,则会出现“不允许405代码方法”的运行时错误:

POST http://localhost:7178/signatures - >返回405状态代码

  • 如果我将RoutePrefix属性命名为“ signature ”,我会收到“400 Bad Request”的运行时错误:

POST http://localhost:7178/signature - >返回400 Bad Request

问题:我不确定“签名”或“签名”是否是RESTful查询字符串的键或反向字。它们在url查询字符串中有特殊含义吗?我希望有人愿意为我解释这个问题。提前谢谢。

当RoutePrefix命名为“标志”时,以下代码正常工作:

[RoutePrefix("sigs")]
public class SignatureApiController : ApiController
    {
        private readonly ISignatureService _signatureService;

        public SignatureApiController(ISignatureService signatureService)
        {
            _signatureService = signatureService;
        }

        [Route("")]
        [HttpPost]
        public HttpResponseMessage CreateSignature(SignatureFormModel sig)
        {
            var val = _signatureService.Save(sig.ImageData, sig.ContactId);
            return val ? Request.CreateResponse(HttpStatusCode.Created, new { ContactId = sig.ContactId, _signatureService.Result }) : Request.CreateErrorResponse(HttpStatusCode.BadRequest, _signatureService.Result);
        }  
    }

但是,当我将RoutePrefix命名为“签名”,“签名”之类的一些键/反转词(?)时,以下代码无效:

[RoutePrefix("signatures")]
    // [RoutePrefix("signature")]
        // Note: RoutePrefix named "signatures" causes error '405: Method Not Allowed' ; RoutePrefix named "signature"  causes error: '400: Bad Request'
        public class SignatureApiController : ApiController
        {
            private readonly ISignatureService _signatureService;

            public SignatureApiController(ISignatureService signatureService)
            {
                _signatureService = signatureService;
            }

            [Route("")]
            [HttpPost]
            public HttpResponseMessage CreateSignature(SignatureFormModel sig)
            {
                var val = _signatureService.Save(sig.ImageData, sig.ContactId);
                return val ? Request.CreateResponse(HttpStatusCode.Created, new { ContactId = sig.ContactId, _signatureService.Result }) : Request.CreateErrorResponse(HttpStatusCode.BadRequest, _signatureService.Result);
            }  
        }

0 个答案:

没有答案