SO中有太多問題與此類似,但沒有解決我的問題。執行PUT/POST時沒有HTTP資源錯誤[CORS問題 - AngularJS + Web API 2]
我已經在http://localhost:49595
託管了一個angularjs網站,我正在調用託管在http://localhost:63019
處的Web Api 2.1服務。我在web.config中的web api服務器上啓用了CORS。請求GET/buildings/1和GET/buildings /?$ top = 10 & $ skip = 1正常工作。
當談到說,我得到以下錯誤: { 「消息」:「沒有HTTP資源發現匹配的請求URI‘http://localhost:63019/buildings/1105
’。」 「MessageDetail」:「沒有行動被發現在與請求匹配的控制器「建築物」上。「 }
我可以看到瀏覽器發送OPTIONS請求到服務器,但是要更新的實際數據是不是在請求(小提琴手檢查。不知道這是正常的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);
也
$http({
url: urlBase + '/' + building.BuildingId,
dataType: 'json',
method: 'PUT',
data: building,
headers: {"Content-Type": "application/json"}
}).then(onSuccess, onError);
這些都是在我的控制器:
[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;
}
我的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>
我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")));
}
找到NuGet包的
Microsoft ASP.NET Web API 2.2 Cross-Origin
我發現,從配置刪除設置並明確註冊方法定義它們爲我工作。 嘗試從所有權限(即*)開始,並從那裏開始工作....確保不要將其部署到生產:) – link64