Put方法不可用(或在帮助中可见)

时间:2015-11-17 13:47:49

标签: c# .net asp.net-mvc asp.net-web-api2

我有一个带有以下PUT方法的Web API 2控制器(PersonProfessionController):

[HttpPut]
[AcceptVerbs("PUT")]
public void Put([FromBody]PersonProfessionModel value)
{
    try
    {
        this.MyProfessionManager.Save(value.ToPersonProfession());
    }
    catch (Exception ex)
    {
        this.LogManager.Error(ex);
    }
}

出于某种原因,它只是不可用。它也在api帮助概述页面中不可见。

另一方面,我的PersonController中有一个方法,如下所示:

[HttpPut]
[AcceptVerbs("PUT")]
public void Put([FromBody]PersonModel value)
{
    try
    {
        this.MyPersonManager.Save(value.ToPerson());
    }
    catch (Exception ex)
    {
        this.LogManager.Error(ex);
    }
}

这很好用。

问题

尽管PersonController的PUT方法看起来与我的PersonProfessionController非常相似,但除了明显的差异之外,前者有效,后者则不然(甚至不可见)。

知道发生了什么事吗?

更新

每个请求的完整PersonProfessionController类:

public class PersonProfessionController : ApiController
{
    private IPersonProfessionManager PersonProfessionManager { get; set; }
    private ILogManager LogManager { get; set; }

    public PersonProfessionController(
        IPersonProfessionManager PersonProfessionManager,
        ILogManager logManager)
    {
        this.PersonProfessionManager = PersonProfessionManager;
        this.LogManager = logManager;
    }

    [HttpGet]
    public IHttpActionResult Get(string languageCode, Guid id)
    {
        try
        {
            var result = this.PersonProfessionManager.Get(languageCode, id);

            if (result == null)
                return NotFound();

            return Ok(result);
        }
        catch (Exception ex)
        {
            this.LogManager.Error(ex);
        }

        return NotFound();
    }

    #region Save

    // PUT api/PersonProfession/save/
    [HttpPut]
    [AcceptVerbs("PUT")]
    public void Put([FromBody]PersonPersonProfessionModel value)
    {
        try
        {
            this.PersonProfessionManager.Save(value.ToPersonProfession());
        }
        catch (Exception ex)
        {
            this.LogManager.Error(ex);
        }
    }

    #endregion

    [HttpGet]
    public IHttpActionResult LoadPersonProfessionsForPersonAndDossier(
        string languageCode,
        Guid dossierId,
        Guid personId
        )
    {
        try
        {
            var contextInformation = new ContextInformation
            {
                LanguageCode = languageCode,
                DossierId = dossierId,
                PersonId = personId
            };

            var result = this.PersonProfessionManager.LoadPersonProfessionsForPersonAndDossier(contextInformation);

            if (result == null)
                return NotFound();

            return Ok(result);
        }
        catch (Exception ex)
        {
            this.LogManager.Error(ex);
        }

        return NotFound();
    }
}

我手动修改了代码以使其品牌中立,如果我改变了某些内容,这可能会导致不一致。

根据请求路由:

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

config.Routes.MapHttpRoute(
    name: "LoadPersonProfessionsForPersonAndDossier",
    routeTemplate: "api/personprofession/LoadPersonProfessionsForPersonAndDossier/{languageCode}/{dossierId}/{personId}",
    defaults: new
    {
        controller = "PersonProfession",
        action = "LoadPersonProfessionsForPersonAndDossier",
        languageCode = RouteParameter.Optional,
        dossierId = RouteParameter.Optional,
        personId = RouteParameter.Optional
    });

config.Routes.MapHttpRoute(
    name: "PersonProfession Put",
    routeTemplate: "api/personprofession/save/",
    defaults: new { controller = "PersonProfession", action = "Put" }
);

1 个答案:

答案 0 :(得分:0)

发现它!这不起作用:

[HttpPut]
[AcceptVerbs("PUT")]
[Route("api/personprofession/save/")]
public void Put([FromBody]PersonProfessionModel value)

这项工作:

[HttpPut]
[Route("api/personprofession/save/")]
public void Put([FromBody]PersonProfessionModel value)

显然你不应该添加[AcceptVerbs("PUT")]

背后的为什么让我感到困惑,但是你走了。