简体   繁体   English

Web Api和ASP.Net MVC

[英]Web Api and ASP.Net MVC

Hello guys I'm new in here. 大家好,我是新来的。 I have a little problem this my HomeController and I will do Get and Create with web API but I can't find and write a Edit / Details and Delete. 我的HomeController有点问题,我将使用Web API进行“获取和创建”,但找不到并编写“编辑/详细信息和删除”。

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {

        List<Product1Model> lst =new List<Product1Model>();
        HttpClient Client = new HttpClient();
        Client.BaseAddress = new Uri("http://localhost:19440/");

        Client.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json") );
        HttpResponseMessage responsive = Client.GetAsync("api/Product1").Result;
        if (responsive.IsSuccessStatusCode)
        {
            lst = responsive.Content.ReadAsAsync<List<Product1Model>>().Result;
        }
        return View(lst);
    }

    public ActionResult Create()
    {
      return View();
    }

    [HttpPost]
    public ActionResult Create(Product1Model model)
    {
        HttpClient Client = new HttpClient();
        Client.BaseAddress = new Uri("http://localhost:19440/");

        Client.PostAsJsonAsync<Product1Model>("api/Product1",model)
            .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
        return RedirectToAction("index");
    }

}
public ActionResult Edit(YourModel model)
{
   call your edigint logc
  return View();
}
public ActionResult Details(int itemId)
    {
       YourModel = new ModelType();
        YourModel = Call your Detail Logic

       return view(YourModel  )          
    }

Given the current structure of the example you just need to rinse an repeat the pattern you are currently using for in Create and Index for Edit and Delete. 给定示例的当前结构,您只需要重复创建和编辑和删除索引中当前使用的模式即可。

Assuming you are following the convention, 假设您遵守惯例,

For Details/Edit you need to create your View in the Views/Home/Detail.cshtml and then add Action methods to handle the GET and POST requests... 对于Details / Edit,您需要在Views/Home/Detail.cshtml创建View,然后添加Action方法来处理GET和POST请求...

//GET Home/Detail/5
public async Task<ActionResult> Detail(int id) {
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:19440/");
    client.DefaultRequestHeaders
        .Accept
        .Add(new MediaTypeWithQualityHeaderValue("application/json"));
    string endpoint = string.Format("api/Product1/{0}", id);
    var response= await client.GetAsync(endpoint);
    if (response.IsSuccessStatusCode) {
        var model = response.Content.ReadAsAsync<Product1Model>();
        return View(model);
    }
    return HttpNotFound();
}

//POST Home/Detail/5
[HttpPost]
public async Task<ActionResult> Detail(int id, Product1Model model) {
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:19440/");
    string endpoint = string.Format("api/Product1/{0}", id);
    await client.PostAsJsonAsync<Product1Model>(endpoint, model)
        .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
    return RedirectToAction("Index");
}

For delete it's little trickier as you have to decide what Http method to use from your view. 对于删除来说,它有点麻烦,因为您必须从视图中决定要使用哪种Http方法。 however your call to the web api would probably be expecting a DELETE method. 但是,您对网络api的调用可能期望使用DELETE方法。 You could stick with convention and use HttpDelete all the way like... 您可以坚持约定,并像这样一直使用HttpDelete。

//DELETE Home/Delete/5
[HttpDelete]
public async Task<ActionResult> Delete(int id) {
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:19440/");
    client.DefaultRequestHeaders
        .Accept
        .Add(new MediaTypeWithQualityHeaderValue("application/json"));
    string endpoint = string.Format("api/Product1/{0}", id);
    var responsive = await client.DeleteAsync(endpoint);
    if (responsive.IsSuccessStatusCode) {
        return RedirectToAction("Index");
    }

    return HttpNotFound();
}

You need to make sure the client (View) uses the correct http method when making the request 您需要确保发出请求时客户端(视图)使用正确的http方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM