![](/img/trans.png)
[英]How can I read the file data from an ASP.NET web API post request?
[英]How I can read parameter value from controller constructor in asp.net web API 2?
我如何從asp.net Web API 2中的控制器構造函數讀取參數值?
public class DataController : ApiController
{
private APIMgr apiMgr ; // APIMgr custome class
public DataController()
{
// var id = Request.GetRouteData(); = 5 // this parameter must send with alla request "http://localhost/TAPI/api/data/5"
apiMgr= new apiMgr(id);
}
構造控制器類時未設置HttpContext,但稍后由ControllerBuilder類設置(“注入”)了。
據此,我可以通過重寫“ Initialize”方法來訪問HttpContext。 本頁說明ASP.NET MVC請求流程
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
}
您不能從構造函數中讀取路由參數,而只能從操作方法中讀取。 您需要使用期望的模板定義適當的路線。 在WebApiConfig.cs中查看路由配置中的默認控制器
同樣,不能從控制器構造函數中訪問HttpContext。
你不能 要在操作方法中讀取參數值,有幾種方法。 讓我們假設一個代表一個人的模型。
public class Person
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
現在讓我們假設我們擁有一個API方法,該方法使我們能夠將人的表示發送到服務器,然后將其保存到某些數據存儲中。 為此,我們實現了一個POST方法,該方法接受參數來構建我們的person對象。 我們有幾種方法可以做到這一點。
方法1:綁定請求正文中的參數
public IHttpActionResult Post([FromBody]Person person)
{
// validate your parameter in some way
if (person.Equals(default(Person))) return BadRequest("person must not be null");
// go off and save the person
var createdPerson = myPersonRepository.Save(person);
if (createdPerson == default(Person)) return InternalServerError();
return CreatedAtRoute("DefaultApi", new { id = createdPerson.Id }, createPerson);
}
這要求您在請求正文中傳遞您的人員的JSON表示形式。 應該執行以下操作。
{
"firstname": "Luke",
"lastname": "Skywalker"
}
方法2:綁定請求URL查詢字符串中的參數
public IHttpActionResult Post([FromUri]Person person)
{
// validate your parameter in some way
if (person.Equals(default(Person))) return BadRequest("person must not be null");
// go off and save the person
var createdPerson = myPersonRepository.Save(person);
if (createdPerson == default(Person)) return InternalServerError();
return CreatedAtRoute("DefaultApi", new { id = createdPerson.Id }, createPerson);
}
這要求您在查詢字符串中傳遞參數值,例如:
HTTP:// mywhizzyapi / API /人姓=盧克和姓=天行者
方法3:顯式傳遞參數並自己創建對象
public IHttpActionResult Post(string firstname, string lastname)
{
// validate your parameter in some way
if (id.Equals(Guid.Empty)) return BadRequest("id must not be null or an empty GUID");
if (string.IsNullOrEmpty(firstname)) return BadRequest("firstname must not be null or empty");
if (string.IsNullOrEmpty(lastname)) return BadRequest("lastname must not be null or empty");
// create your person object
var person = New Person {
id = id,
firstName = firstname,
lastname = lastname,
};
// go off and save the person
var createdPerson = myPersonRepository.Save(person);
if (createdPerson == default(Person)) return InternalServerError();
return CreatedAtRoute("DefaultApi", new { id = createdPerson.Id }, createPerson);
}
此方法還要求您在查詢字符串中傳遞參數值,例如:
HTTP:// mywhizzyapi / API /人姓=盧克和姓=天行者
但是在最后提到的情況下,您必須將參數顯式綁定到模型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.