【问题标题】:Web Api and ASP.Net MVCWeb Api 和 ASP.Net MVC
【发布时间】:2016-06-09 13:29:10
【问题描述】:

大家好,我是新来的。我的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");
    }

}

【问题讨论】:

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


    【解决方案1】:

    鉴于示例的当前结构,您只需要冲洗并重复您当前在创建和索引中用于编辑和删除的模式。

    假设您遵循约定,

    有关详细信息/编辑,您需要在 Views/Home/Detail.cshtml 中创建您的视图,然后添加操作方法来处理 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");
    }
    

    对于删除,它有点棘手,因为您必须从您的视图中决定使用哪种 Http 方法。但是您对 web api 的调用可能需要一个 DELETE 方法。您可以坚持约定并一直使用 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();
    }
    

    您需要确保客户端(View)在发出请求时使用正确的http方法

    【讨论】:

      【解决方案2】:
      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  )          
          }
      

      【讨论】:

        猜你喜欢
        • 2021-10-27
        • 1970-01-01
        • 2023-03-22
        • 2014-05-27
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多