调用Web Api的ashx处理程序时出错404

时间:2015-09-11 20:19:38

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

我的WebApi项目中有一个slice处理程序。源代码可用here。我试图从我的AngularJs项目中调用我的处理程序。这是代码:

_invoice_pdf.html.erb

HTML

ImageUploadHandler.ashx

调用此处理程序时出现404错误,这是错误快照:

enter image description here

1 个答案:

答案 0 :(得分:0)

这就是诀窍:

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ClearHeaders();
            string origin = context.Request.Headers["Origin"];
            context.Response.AppendHeader("Access-Control-Allow-Origin",
                string.IsNullOrEmpty(origin) ? "*" : origin);
            string requestHeaders = context.Request.Headers["Access-Control-Request-Headers"];
            context.Response.AppendHeader("Access-Control-Allow-Headers",
                string.IsNullOrEmpty(requestHeaders) ? "*" : requestHeaders);
            context.Response.AppendHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
        }

另外,我必须在web.config

中注册我的处理程序
<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
    </modules>
    <handlers>
        <add name="ImageUploadHandler" verb="*" path="ImageUploadHandler.ashx" type="JetAdz.WebApi.ImageUploadHandler, JetAdz.WebApi, Version=1.0.0.0, Culture=neutral" />
    </handlers>
</system.webServer>