WebApiコントローラを作成する必要があります。タスクは、通常のMVCコントローラからWebApiコントローラに移行することです。WebApiコントローラにモデルを渡すには?
- デザインパターン - - リポジトリ+ UOW、工場+ NUnitの
- 依存性注入
- SOLID
は、プログラミングのほぼすべての美しさを同じように使用されます
これはMyApp.Domain
層での私のモデルである
public class Customer
{
//please note that we are using Repository pattern & so no Data Annotations like Required, Key are not used here.
public int CustomerID { get; set;}
public string CustomerName { get; set;}
public string EmailAddress { get; set;}
// & so on
}
層で
は、サービス層とモデルを渡す検証&ためのビューモデルが存在します。これは私のMVCコントローラの外観です
public class CustomerVM
{
[Required]
public int CustomerID { get; set;} // & so on the other properties.
}
public ActionResult Registration(Customer VM)
{
if(Modelstate.IsValid)
{
//call service layer
}
else
{
}
}
このコントローラをWebApiコントローラにマイグレートすることです。 今後、私は別のプロジェクトを作成しました。MyApp.WebApi
このWebApiコントローラにモデルを渡すべきかどうか疑いがあります。
私は、UI層& WEBAPI層でDLLをMyApp.ViewModels
として プロジェクトを分離し、この層に &の参照をすべてのviewmodelsを置くためにUI層からのViewModelを切り離すために考えています。
public string POST([FromBody]CustomerVM customer)
{
if(Modelstate.IsValid)
{
//call the other service layer which will take care of DB handling
return "Success";
}
else
{
return "Error";
}
}
これは、これを行うには、他の正しい方法を&行うには正しい方法です?? RESTfulなAPIで
これを行うと問題はありません。 –
@ YashveerSingh、これであなたは確かですか? –
これは私が何を理解した場合。既存のMVCプロジェクトをWebApiに変換します。したがって、VCプロジェクトで使用していたように、既存のビューモデルを使用することができます。web apiはmvcのようにモデルバインドを実行できます。 –