繁体   English   中英

Web API JSON导致未找到404

[英]Web API JSON resulting in 404 Not Found

我不知道发生了什么,但是我觉得这是路由问题。

这是JSON

  $("form").submit(function () {
          if (api.validateForm()) {
                 var pid = '1198';
                 var image = document.getElementById("myCanvas").toDataURL("image/png");
                 image = image.replace('data:image/png;base64,', '');
                 $.ajax({
                            type: 'POST',
                            async: false,
                            url: 'https://localhost:44301/webapi/SaveImage/SaveSignature',
                            data: '{ "imageData" : "' + image + '" }',
                            contentType: 'application/json; charset=utf-8',
                            dataType: 'json',
                            success: function (msg) {
                                window.location.replace("success.aspx");
                            }
                  });

          }
          return false;
    });

申请开始

    RouteTable.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "webapi/{controller}/{action}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional }
        );

这是我的控制器

public class SaveImageController : ApiController
    {
        public class CrossDomainActionFilter : ActionFilterAttribute
        {
            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
                bool needCrossDomain = true;

                if (needCrossDomain)
                {
                    actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                }

                base.OnActionExecuted(actionExecutedContext);
            }
        }

        [AcceptVerbs("GET", "POST")]
        [CrossDomainActionFilter]
        public bool SaveSignature(string imageData)
        {
            string uploadPath = HttpContext.Current.Server.MapPath("Files") + "\\1198\\";

            if (!System.IO.Directory.Exists(uploadPath))
            {
                System.IO.Directory.CreateDirectory(uploadPath);
            }

            string fileNameWitPath = uploadPath + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
            using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    byte[] data = Convert.FromBase64String(imageData);
                    bw.Write(data);
                    bw.Close();
                }
            }

            return true;
        }
        [AcceptVerbs("Get")]
        public string SayHello(string aye)
        {
            return "Say Hello from Test Controller";
        }

    }

Firebug错误响应

{"Message":"No HTTP resource was found that matches the request URI 'https://localhost:44301/webapi/SaveImage/SaveSignature'.","MessageDetail":"No action was found on the controller 'SaveImage' that matches the request."}

很有可能是https-您是否在https中运行开发环境? 尝试仅在您的网址中使用http。

Web Api无法处理此类请求。 我使用querystrings传递了它,并且起作用了,所以现在我将通过创建一个Signature对象并插入信息并传递该对象来传递给api。

此链接将帮助其他任何人解决此问题

http://techbrij.com/pass-parameters-aspdotnet-webapi-jquery

暂无
暂无

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

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