首页 > 解决方案 > 路线在邮递员中无法正常工作,因此不允许发布

问题描述

我正在使用 mvc 和 Visual Studio 创建一个 Web api,但是我遇到了一个问题。我通过本地主机而不是浏览器的方式在 post man 的桌面版本上运行它。

我调用网址的方式是

http://localhost:12513/api/CustomerContracts/AddCustomer

[System.Web.Http.HttpPost]
[System.Web.Http.Route("AddCustomer/{description}/{customerRef}/{contractTypeId}/{startDate}/{EndDate}")]

public async Task AddCustomer(string Description, string CustomerRef, int ContractTypeId, DateTime startDate, DateTime endDate)
{
        CustomerContracts _newContact = new CustomerContracts();

        try
        {
            _newContact.Description = Description;
            _newContact.CustomerRef = CustomerRef;
            _newContact.ContractTypeId = ContractTypeId;
            _newContact.StartDate = startDate;
            _newContact.EndDate = endDate;
            db.Entry(_newContact).State = EntityState.Modified;

            db.CustomerContract.Add(_newContact);
            await db.SaveChangesAsync();
        }
        catch (Exception x)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(x.Message) });


        }           

 }

当我在邮递员中运行上述内容时,它给了我以下错误

{ "Message": "请求的资源不支持 http 方法 'POST'。" }

我在同一个问题上尝试了另一个 SO,但它没有解决我的问题。

我使用的是第一次创建时位于 web api 项目中的默认路由文件。

 public static void RegisterRoutes(RouteCollection routes)
 {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
 }

编辑 2

我提出了以下建议以及邮递员中错误消息的相同结果。

 [System.Web.Http.HttpPost]


[System.Web.Http.Route("AddCustomer/{Description}/
{CustomerRef}/{ContractTypeId}/{startDate}/{endDate}")]

public async Task AddCustomer(string Description, string CustomerRef, int ContractTypeId, DateTime startDate, DateTime endDate)
    {



        CustomerContracts _newContact = new CustomerContracts();

        try
        {
            _newContact.Description = Description;
            _newContact.CustomerRef = CustomerRef;
            _newContact.ContractTypeId = ContractTypeId;
            _newContact.StartDate = startDate;
            _newContact.EndDate = endDate;
            db.Entry(_newContact).State = EntityState.Modified;

            db.CustomerContract.Add(_newContact);
            await db.SaveChangesAsync();
        }
        catch (Exception x)
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(x.Message) });


        }



    }

标签: c#httpasp.net-web-apipostman

解决方案


必须与您编写列表名称的routing attributes方式完全相同。parameters

所以而不是:

[System.Web.Http.Route("AddCustomer/{description}/{customerRef}/{contractTypeId}/{startDate}/{EndDate}")]

它应该是

[System.Web.Http.Route("AddCustomer/{Description}/{CustomerRef}/{ContractTypeId}/{startDate}/{endDate}")]

请注意我如何更改路径属性以匹配下面的参数列表。

public async Task AddCustomer(string Description, string CustomerRef, int ContractTypeId, DateTime startDate, DateTime endDate)
{


 }

第二:你需要WebApiConfig.cs添加App_Start

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

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

然后:在Global.asaxfile inApplication_Start方法中,添加以下行来注册WebApi Router

GlobalConfiguration.Configure(WebApiConfig.Register);

推荐阅读