不允许asp.net网页API 405的方法,张贴,自定义操作 [英] asp.net web api 405 method not allowed, post, custom action

查看:413
本文介绍了不允许asp.net网页API 405的方法,张贴,自定义操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的同一个问​​题很多的问题,但我真的想不通,我做错了什么。
我收到一个405错误,当我发送到InscricaoSecretaria / Salvar方法的请求。

下面是我的配置文件:

 公共静态类WebApiConfig
{
    公共静态无效的注册(HttpConfiguration配置)
    {
        config.MessageHandlers.Add(新AuthorizationHandler());        //默认路由配置        config.Routes.MapHttpRoute(
            名称:DefaultApi2
            routeTemplate:API / {控制器} / {行动} / {ID}
            默认:新{ID = RouteParameter.Optional}
        );        config.Routes.MapHttpRoute(
            名称:DefaultApi
            routeTemplate:API / {}控制器/(编号),
            默认:新{ID = RouteParameter.Optional}
        );        //团结容器配置        IUnityContainer集装箱=新UnityContainer();
        container.RegisterType< BusinessContext>(新InjectionConstructor(ConfigurationManager.ConnectionStrings [HabileConnection]的ConnectionString));
        config.DependencyResolver =新UnityResolver(容器);
    }
}

下面是我的控制器

 公共类InscricaoSecretariaController:ApiController
{
    [HttpPost]
    公众的Htt presponseMessage Salvar(InscricaoSecretaria inscricao)
    {
        返回null;
    }
}

和Ajax调用(angularjs)。我使用名为角模块salvar的方法,它是通过单击按钮解雇了。

 使用严格的;(功能(){
    VAR RESTClient实现= angular.module(RESTClient实现,[ngResource]);
    VAR的serviceURL =/ habileapp / API /;    restClient.factory(InscricaoResource,[$资源功能($资源){
        返回$资源(serviceURL中+InscricaoSecretaria,空,{
            recuperar:{
                方法:取
            },
            salvar:{
                方法:后
                URL:serviceURL中+inscricaoSecretaria / Salvar
            },
            finalizar:{
                方法:后
                URL:serviceURL中+inscricaoSecretaria / Finalizar
            }
        });
    }]);
})();

服务器与此错误消息进行响应:

 所请求的资源不支持HTTP方法POST。

编辑:
下面是一些请求头:

 请求URL:http://本地主机/ habileapp / API / inscricaoSecretaria / Salvar
请求方法:POST
状态code:405不允许的方法


解决方案

您确定您的Web服务器的设置已被配置来处理POST动词?默认情况下,应该是设置为GET和POST请求,但它可能会在某个时候被改变。下面是一些步骤来检查:

IIS:
<一href=\"http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications\" rel=\"nofollow\">http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

IIS防爆preSS:
看在applicationHost.config文件中的%USERPROFILE%\\文件\\ IISEx preSS \\ config文件夹。检查ExtensionlessUrl - 集成 - 4.0处理程序这样的动词配置的东西=GET,HEAD,POST,DEBUG,PUT,DELETE

I've seen many question about the same issue, but i really can't figure out what I'm doing wrong. I receive a 405 error when I send a request to the InscricaoSecretaria/Salvar method.

Here is my config file:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MessageHandlers.Add(new AuthorizationHandler());

        // Default Route Configuration

        config.Routes.MapHttpRoute(
            name: "DefaultApi2",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // Unity Container Configuration

        IUnityContainer container = new UnityContainer();
        container.RegisterType<BusinessContext>(new InjectionConstructor(ConfigurationManager.ConnectionStrings["HabileConnection"].ConnectionString));
        config.DependencyResolver = new UnityResolver(container);
    }
}

Here is my controller

public class InscricaoSecretariaController : ApiController
{
    [HttpPost]
    public HttpResponseMessage Salvar(InscricaoSecretaria inscricao)
    {
        return null;
    }
}

and the ajax call (angularjs). I am using the method named "salvar" of the angular module, which is fired with a button click.

"use strict";

(function(){
    var restClient = angular.module("restClient", ["ngResource"]);
    var serviceURL = "/habileapp/api/";

    restClient.factory("InscricaoResource", ["$resource", function ($resource) {
        return $resource(serviceURL + "InscricaoSecretaria", null, {
            "recuperar": {
                "method": "get"
            },
            "salvar": {
                "method": "post",
                "url": serviceURL + "inscricaoSecretaria/Salvar"
            },
            "finalizar": {
                "method": "post",
                "url": serviceURL + "inscricaoSecretaria/Finalizar"
            }
        });
    }]);
})();

The server responds with this error message:

The requested resource does not support http method 'POST'.

EDIT: Here are some request headers:

Request URL:http://localhost/habileapp/api/inscricaoSecretaria/Salvar
Request Method:POST
Status Code:405 Method Not Allowed

解决方案

Are you sure that your webserver setup has been configured to handle the POST verb? By default it should be setup for both GET and POST requests but it may have been changed at some point. Below are some steps to check:

IIS: http://www.asp.net/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

IIS Express: Look in the applicationHost.config file in %userprofile%\documents\IISExpress\config folder. Check that the ExtensionlessUrl-Integrated-4.0 handler is configured something like this verb="GET,HEAD,POST,DEBUG,PUT,DELETE

这篇关于不允许asp.net网页API 405的方法,张贴,自定义操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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