【问题标题】:POST Data from MVC View by using the web api使用 web api 从 MVC 视图 POST 数据
【发布时间】:2018-05-11 02:44:27
【问题描述】:

我正处于 Web API 的学习阶段,我想使用 WebAPI 从视图中将数据发布到数据库中。如何从 MVC 中访问 WebAPI。

这里是控制器

[HttpPost]
public ActionResult PatientMedicines(DomainModels.Orders.Medicines medicine, string key, string username, string FullName, string Phone, string CNIC, string address, string Email, DateTime dateofbirth, string Gender, string PaymentMethod, string PostedPrescription)
{
    SessionUser user = Session["User"] as SessionUser;
    medicine.add_with_api( key,user.Username, FullName,  Phone,  CNIC, address, Email, dateofbirth, Gender, PaymentMethod, PostedPrescription);//
    return View(new ViewModels.Orders.Medicines(user.Username));
}

现在是模型

string baseUrl = ServerConfig.server_path + "/api/Payment/AddMedicineOrder";
Dictionary<string,string> parameters = new Dictionary<string,string>();

parameters.Add("username",username);
parameters.Add("FullName", FullName);
parameters.Add("Phone",Phone);
parameters.Add("CNIC",CNIC);
parameters.Add("address",address);
parameters.Add("Email",Email);
parameters.Add("dateofbirth",dateofbirth.ToShortDateString());
parameters.Add("Gender",Gender);
parameters.Add("PaymentMethod",PaymentMethod);
parameters.Add("image","null");
var response =Common.ReadFromAPI<API(baseUrl,"Post",parameters);
return true;

如何使用POST方法通过api插入数据?

【问题讨论】:

标签: c# asp.net-mvc asp.net-web-api


【解决方案1】:

您不应该使用您的模型来执行此操作。您应该创建一个负责调用 API 的类。这里有很多例子解释了如何做到这一点:

How to call API in asp.net MVC5

how to call web api or other restful web services in c#

Making a simple C# API call

这里是一个从控制器调用 api 的简单示例:

public class HomeController : Controller  
{  
    //Hosted web API REST Service base url  
    string Baseurl = "http://xxx.xxx.xx.x:xxxx/";      
    public async Task<ActionResult> Index()  
    {  
        List<Employee> EmpInfo = new List<Employee>();  

        using (var client = new HttpClient())  
        {  
            //Passing service base url  
            client.BaseAddress = new Uri(Baseurl);  

            client.DefaultRequestHeaders.Clear();  
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  

            //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
            HttpResponseMessage Res = await client.GetAsync("api/Employee/GetAllEmployees");  

            //Checking the response is successful or not which is sent using HttpClient  
            if (Res.IsSuccessStatusCode)  
            {  
                //Storing the response details recieved from web api   
                var EmpResponse = Res.Content.ReadAsStringAsync().Result;  

                //Deserializing the response recieved from web api and storing into the Employee list  
                EmpInfo = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse);  

            }  
            //returning the employee list to view  
            return View(EmpInfo);  
        }  
     }  
}  

【讨论】:

    猜你喜欢
    • 2019-10-02
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多