gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:57:35 26 4
gpt4 key购买 nike

我有一个具有以下 PUT 方法的 Web API 2 Controller (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" }
);

最佳答案

找到了!这不起作用:

[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")]

它背后的原因让我感到困惑,但现在开始吧。

关于c# - Put 方法不可用(或在帮助中可见),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33758571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com