2016-03-07 66 views
2

我有一個通過電子郵件發送給用戶的報告(因爲需要一些時間才能創建)。我無法調用web方法並傳遞視圖模型。如何將視圖模型從控制器傳遞給webapi方法

這是的WebAPI控制器:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 

namespace RecognitionsMVC.Controllers 
{ 
    public class WebAPIController : ApiController 
    { 
     [ActionName("Post")] 
     public static void GetAllRecognitionsBySupervisorAll([FromUri]ViewModels.AllRecognitionsbyAllSupervisors AllRByAllS) 
     { 
      DataSet ds = Classes.Recognitions.GetAllRecognitionsBySupervisorAll(AllRByAllS.BeginDate, AllRByAllS.EndDate, AllRByAllS.RecognizedOrSubmitted); 
      Classes.DataHelper.SendMeExcelFile(ds, "GetAllRecognitionsBySupervisorAll", AllRByAllS.AuthenticatedUser); 
     } 

    } 
} 

這是我使用的視圖模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace RecognitionsMVC.ViewModels 
{ 
    public class AllRecognitionsbyAllSupervisors 
    { 
     public DateTime BeginDate { get; set; } 
     public DateTime EndDate { get; set; } 
     public string AuthenticatedUser { get; set; } 
     public bool RecognizedOrSubmitted { get; set; } 

     public AllRecognitionsbyAllSupervisors(DateTime BeginDate, DateTime EndDate, string AuthenticatedUser, bool RecognizedOrSubmitted) 
     { 
      this.BeginDate = BeginDate; 
      this.EndDate = EndDate; 
      this.AuthenticatedUser = AuthenticatedUser; 
      this.RecognizedOrSubmitted = RecognizedOrSubmitted; 
     } 

    } 

} 

,這是測試控制器我試圖調用的WebAPI控制器和傳遞查看模型:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace RecognitionsMVC.Controllers 
{ 
    public class TestController : Controller 
    { 
     // GET: Test 
     public ActionResult Index() 
     { 
      DateTime BeginDate = new DateTime(2015, 1, 1); 
      DateTime EndDate = new DateTime(2015, 12, 31); 
      string AuthenticatedUser = "123473306"; 
      bool RecognizedOrSubmitted = true; 

      string VPath = "api/WebAPI/GetAllRecognitionsBySupervisorAll"; 
      ViewModels.AllRecognitionsbyAllSupervisors AllRByAllS = new ViewModels.AllRecognitionsbyAllSupervisors(BeginDate, EndDate, AuthenticatedUser, RecognizedOrSubmitted); 
      return View(VPath, AllRByAllS); 

     } 
    } 
} 

最後,這是App_Start文件夾中的WebApiConfig.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Http; 

namespace RecognitionsMVC 
{ 
    public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // TODO: Add any additional configuration code. 

      // Web API routes 
      config.MapHttpAttributeRoutes(); 

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

      // WebAPI when dealing with JSON & JavaScript! 
      // Setup json serialization to serialize classes to camel (std. Json format) 
      var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; 
      formatter.SerializerSettings.ContractResolver = 
       new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); 

     } 
    } 
} 

該錯誤表明api無法找到,並且看起來是在查看所有視圖文件夾而不是WebAPI控制器。我如何調用WebAPI方法並傳遞視圖模型?

回答

1

在您的控制器中,您可以使用HttpClient來調用Web api。

public class TestController : Controller 
{ 
    // GET: Test 
    public async Task<ActionResult> Index() 
    { 
     DateTime BeginDate = new DateTime(2015, 1, 1); 
     DateTime EndDate = new DateTime(2015, 12, 31); 
     string AuthenticatedUser = "123473306"; 
     bool RecognizedOrSubmitted = true; 

     string VPath = "api/WebAPI/GetAllRecognitionsBySupervisorAll"; 
     ViewModels.AllRecognitionsbyAllSupervisors AllRByAllS = new ViewModels.AllRecognitionsbyAllSupervisors(BeginDate, EndDate, AuthenticatedUser, RecognizedOrSubmitted); 

     var baseUrl = new Uri("http://localhost:1234/");//Replace with api host address 
     var client = new HttpClient();//Use this to call web api 
     client.BaseAddress = baseUrl; 

     //post viewmodel to web api using this extension method 
     var response = await client.PostAsJsonAsync(VPath, AllRByAllS); 

     return View(); 

    } 
} 

您的Web Api也需要更改以從視圖模型中獲取視圖模型。

public class WebAPIController : ApiController 
{ 
    [HttpPost] 
    [ActionName("Post")] 
    public static void GetAllRecognitionsBySupervisorAll([FromBody]ViewModels.AllRecognitionsbyAllSupervisors AllRByAllS) 
    { 
     DataSet ds = Classes.Recognitions.GetAllRecognitionsBySupervisorAll(AllRByAllS.BeginDate, AllRByAllS.EndDate, AllRByAllS.RecognizedOrSubmitted); 
     Classes.DataHelper.SendMeExcelFile(ds, "GetAllRecognitionsBySupervisorAll", AllRByAllS.AuthenticatedUser); 
    } 

} 
+0

該應用程序未知「GetBaseURL」。 (我是一個noob,所以我不知道如何添加,而且傳遞的AllRByAllS視圖模型表明它不能轉換爲System.Net.Http.HttpContent。我嘗試了一次轉換,但沒有成功。這兩個問題? – pldiguanaman

+0

我發現一個GetBaseURL函數,我添加了它似乎解決了第一個問題。仍然不確定從列表中轉換爲HttpContent的錯誤對象 – pldiguanaman

+0

'GetBaseURL'只是一個佔位符方法,您需要實現自己(PostAsync)是'System.Net.Http'命名空間中'HttpClient'的通用擴展方法 – Nkosi

1

我認爲GetAllRecognitionsBySupervisorAll內WebAPIController不允許是靜態的。當你將它標記爲靜態時,它不會被發現,所以請嘗試刪除「靜態」。

相關問題