做PUT当没有HTTP资源错误/ POST [CORS问题 - AngularJS +网页API 2] [英] No HTTP resource error when doing PUT/POST [CORS issue - AngularJS + Web API 2]

查看:377
本文介绍了做PUT当没有HTTP资源错误/ POST [CORS问题 - AngularJS +网页API 2]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在如此的相似,这这么多的问题,但没有一个有固定我的问题。

我已经有在 HTTP主办的网站angularjs://本地主机:49595 和我打电话的托管的网页API 2.1服务HTTP://本地主机:63019 。我已经启用web.config中的网络API服务器上CORS。对于GET请求/建筑/ 1和GET /建筑/ $ TOP = 10安培;。$跳过= 1,做工精细

当谈到说,我得到以下错误:
{
    消息:没有HTTP资源发现,请求URI匹配'的http://本地主机:63019 /建筑/ 1105 '。
    MessageDetail:没有采取任何控制器建设与请求匹配上找到。
}

我可以看到浏览器发送OPTIONS请求到服务器,但要更新的实际数据是不是在请求(小提琴手检查。不知道这是正常的一个OPTIONS查询。

我启用跟踪上的Web API,我看到以下日志。 https://gist.github.com/rnarayana/8905229 OPTIONS请求(第69行)说:它需要做一个PUT。大约第77行,它看起来像它选择了正确的行动。然后控制器得到处理。此外,该控制器被越来越实例的两倍。

我如何才能找到发生了什么事?

调用code在AngularJS:
尝试:

  $ http.put(的http://本地主机:63019 /建筑/+ building.BuildingId,建筑)
。然后(的onSuccess,onError的);

  $ HTTP({
    网址:urlBase +'/'+ building.BuildingId,
    数据类型:JSON,
    方法:把',
    数据:建筑,
    标题:{内容类型:应用/ JSON}
}),然后(的onSuccess,onError的)。

这是在我的控制器:

  [HTTPGET]
[路线(建筑/)]
公共IHttpActionResult GETALL(ODataQueryOptions< BuildingViewModel> queryOptions)
{
}[HTTPGET]
[路线(建筑/(编号))
公共IHttpActionResult获取(长ID)
{
}[路线(建筑/)]
[HttpPost]
公共IHttpActionResult邮政(BuildingEditViewModel buildingEditViewModel)
{
}[HttpPut]
[路线(建筑/(编号))
公共IHttpActionResult把(长ID,[FromBody] BuildingEditViewModel buildingEditViewModel)
{
}//这个添加按照一些博客。没有帮助。
[HttpOptions]
[路线(建筑/(编号))
公众的Htt presponseMessage选项(长ID)
{
    VAR响应=新的Htt presponseMessage();
    response.Status code =的HTTPStatus code.OK;
    返回响应;
}

我的WebAPI的web.config有:

 < system.webServer>
        <模块runAllManagedModulesForAllRequests =真正的>
          <清除NAME =WebDAVModule/>
        < /模块>
        <&处理GT;
          <清除NAME =WebDAV的/>
          <清除NAME =ExtensionlessUrlHandler - 集成 - 4.0/>
          <清除NAME =OPTIONSVerbHandler/>
          <清除NAME =TRACEVerbHandler/>
          <添加名称=ExtensionlessUrlHandler - 集成 - 4.0PATH =*。动词=*类型=System.Web.Handlers.TransferRequestHandlerpreCondition =integratedMode,runtimeVersionv4.0/>
        < /处理器>
        < httpProtocol>
          < customHeaders>
            <添加名称=访问控制允许来源VALUE =HTTP://本地主机:49595/>
            <添加名称=访问控制允许的方法VALUE =GET,POST,OPTIONS,PUT,DELETE/>
            <添加名称=访问控制 - 允许 - 头VALUE =*/>
          < / customHeaders>
        < / httpProtocol>
    < /system.webServer>

我WebApiConfig.Register()有:

 公共静态无效的注册(HttpConfiguration配置)
    {
    config.EnableSystemDiagnosticsTracing();
    config.Services.Replace(typeof运算(ITraceWriter),新WebApiTracer());    //启用CORS。允许的起源和动词是在配置文件中。不要在这两个地方补充。
    config.EnableCors();    //属性的路由。
    config.MapHttpAttributeRoutes();    config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(
        config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault((T => t.MediaType ==应用程序/ XML)));
}


解决方案

我已经得到它与在配置中的值工作的问题,所以我从那里把他们赶走,我增加了以下我WebApiConfig类:

  //指定适当的值(起源,头,方法)
        变种CORS =新EnableCorsAttribute(HTTP:// myurl,*,*);
        config.EnableCors(CORS);

您可以找到的NuGet包微软的ASP.NET Web API 2.2跨域来自的这里

There are so many questions in SO similar to this, but none have fixed my issue.

I've have an angularjs website hosted at http://localhost:49595 and I'm calling a Web Api 2.1 service hosted at http://localhost:63019. I've enabled CORS on the web api server in web.config. Requests for GET /buildings/1 and GET /buildings/?$top=10&$skip=1 work fine.

When it comes to PUT, I get the following error: { "Message":"No HTTP resource was found that matches the request URI 'http://localhost:63019/buildings/1105'.", "MessageDetail":"No action was found on the controller 'Building' that matches the request." }

I can see the browser sends the OPTIONS request to the server, but the actual data to be updated is not in the request (checked in fiddler. Not sure if this is normal for an OPTIONS query.

I enabled tracing on the web api, and I see the following log. https://gist.github.com/rnarayana/8905229 The OPTIONS request (line 69) says it needs to do a PUT. Around line 77, it looks like it picked the correct action. And then the controller gets disposed. Moreover the controller is getting instantiated twice.

How can I find out what is happening?

Calling code in AngularJS: Tried:

$http.put('http://localhost:63019/buildings/' + building.BuildingId, building)
.then(onSuccess, onError);

also

$http({
    url: urlBase + '/' + building.BuildingId,
    dataType: 'json',
    method: 'PUT',
    data: building,
    headers: {"Content-Type": "application/json"}
}).then(onSuccess, onError);

These are in my controller:

[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;
}

My webapi web.config has:

<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>

My WebApiConfig.Register() has:

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")));
}

解决方案

I had issues getting it to work with the the values in the config so I removed them from there and I added the following to my WebApiConfig class:

        //Specify values as appropriate (origins,headers,methods)
        var cors = new EnableCorsAttribute("http://myurl","*","*");
        config.EnableCors(cors);

You can find the nuget package for Microsoft ASP.NET Web API 2.2 Cross-Origin from here

这篇关于做PUT当没有HTTP资源错误/ POST [CORS问题 - AngularJS +网页API 2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆