简体   繁体   中英

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.

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...

//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. however your call to the web api would probably be expecting a DELETE method. You could stick with convention and use HttpDelete all the way like...

//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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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