我有一个向用户发送电子邮件的报告(因为创建需要一些时间。)我无法调用Web方法并传递viewmodel。
这是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方法并传递视图模型?
答案 0 :(得分:2)
在您的控制器中,您可以使用HttpClient
来呼叫网络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);
}
}
答案 1 :(得分:1)
我认为WebAPIController中的GetAllRecognitionsBySupervisorAll不允许是静态的。当您将其标记为静态时,将无法找到它,因此请尝试删除“静态”。