Nar*_*ana 5 put cors asp.net-web-api angularjs
在SO中有这么多问题与此类似,但没有一个问题解决了我的问题.
我已经托管了一个angularjs网站http://localhost:49595,我正在调用一个Web Api 2.1服务http://localhost:63019.我在web.config中的web api服务器上启用了CORS.GET/buildings/1和GET/buildings /?$ top = 10&$ skip = 1的请求工作正常.
说到PUT,我收到以下错误:{"Message":"找不到与请求URI匹配的HTTP资源http://localhost:63019/buildings/1105'.","MessageDetail":"在控制器'Building'上找不到匹配的操作要求." }

我可以看到浏览器将OPTIONS请求发送到服务器,但是要更新的实际数据不在请求中(在fiddler中检查.不确定OPTIONS查询是否正常).

我在web api上启用了跟踪,我看到了以下日志.https://gist.github.com/rnarayana/8905229 OPTIONS请求(第69行)说它需要进行PUT.在第77行附近,看起来它选择了正确的动作.然后控制器被处理掉.此外,控制器正在实例化两次.
我怎样才能知道发生了什么?
在AngularJS中调用代码:尝试:
$http.put('http://localhost:63019/buildings/' + building.BuildingId, building)
.then(onSuccess, onError);
Run Code Online (Sandbox Code Playgroud)
也
$http({
url: urlBase + '/' + building.BuildingId,
dataType: 'json',
method: 'PUT',
data: building,
headers: {"Content-Type": "application/json"}
}).then(onSuccess, onError);
Run Code Online (Sandbox Code Playgroud)
这些在我的控制器中:
[HttpGet]
[Route("buildings/")]
public IHttpActionResult GetAll(ODataQueryOptions<BuildingViewModel> queryOptions)
{
}
[HttpGet]
[Route("buildings/{id}")]
public IHttpActionResult Get(long id)
{
}
[Route("buildings/")]
[HttpPost]
public IHttpActionResult Post(BuildingEditViewModel buildingEditViewModel)
{
}
[HttpPut]
[Route("buildings/{id}")]
public IHttpActionResult Put(long id, [FromBody]BuildingEditViewModel buildingEditViewModel)
{
}
//Added this as per some blog. Not helping.
[HttpOptions]
[Route("buildings/{id}")]
public HttpResponseMessage Options(long id)
{
var response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
return response;
}
Run Code Online (Sandbox Code Playgroud)
我的webapi web.config有:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:49595" />
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
我的WebApiConfig.Register()有:
public static void Register(HttpConfiguration config)
{
config.EnableSystemDiagnosticsTracing();
config.Services.Replace(typeof(ITraceWriter), new WebApiTracer());
//Enable CORS. The allowed origins and verbs are in the config file. Do not add in both places.
config.EnableCors();
// Attribute routing.
config.MapHttpAttributeRoutes();
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(
config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault((t => t.MediaType == "application/xml")));
}
Run Code Online (Sandbox Code Playgroud)
我有问题让它使用配置中的值,所以我从那里删除它们,我将以下内容添加到我的WebApiConfig类:
//Specify values as appropriate (origins,headers,methods)
var cors = new EnableCorsAttribute("http://myurl","*","*");
config.EnableCors(cors);
Run Code Online (Sandbox Code Playgroud)
你可以找到的NuGet包Microsoft ASP.NET Web API 2.2 Cross-Origin从这里
| 归档时间: |
|
| 查看次数: |
7667 次 |
| 最近记录: |