Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 来自web api的更新导致对象引用未设置为对象的实例。错误。列出、添加和删除webapi crud中的工作罚款_Asp.net Core_Client_Webapi - Fatal编程技术网

Asp.net core 来自web api的更新导致对象引用未设置为对象的实例。错误。列出、添加和删除webapi crud中的工作罚款

Asp.net core 来自web api的更新导致对象引用未设置为对象的实例。错误。列出、添加和删除webapi crud中的工作罚款,asp.net-core,client,webapi,Asp.net Core,Client,Webapi,我正在使用.NET3.1内核中的各种技术。crud在mvc和razor页面的脚手架中都能很好地工作,正如人们所期望的那样。我还一直在使用微软标准的webapi脚手架和与crud相关的postman中的Everything来试验webapi 我一直在尝试让web应用程序客户端和webapi以及blazor web组件进行对话。两者都与网格中的初始数据列表以及添加和删除相关。但更新总是会出现上述错误。下面是用于添加和更新的简短html,以及客户端中控制器的代码,该控制器随后与webapi对话 加 @

我正在使用.NET3.1内核中的各种技术。crud在mvc和razor页面的脚手架中都能很好地工作,正如人们所期望的那样。我还一直在使用微软标准的webapi脚手架和与crud相关的postman中的Everything来试验webapi

我一直在尝试让web应用程序客户端和webapi以及blazor web组件进行对话。两者都与网格中的初始数据列表以及添加和删除相关。但更新总是会出现上述错误。下面是用于添加和更新的简短html,以及客户端中控制器的代码,该控制器随后与webapi对话

@model Prdsrv
@{Layout=“\u Layout”ViewBag.Title=“添加产品服务”}
重新添加预订
产品服务说明:
最近更新:
简要说明:
说明:
添加
@查看包。结果
@如果(型号!=null)
{
产品或服务
产品服务ID
产品服务说明
最后更新
简短描述
中等描述
@Prodservid模型
@Prodservdesc模型
@模型。上次更新
@Model.Serviceshortdesc
@Model.Servicedesc
}
编辑

@model Prdsrv
@{Layout=“\u Layout”ViewBag.Title=“更新产品服务”}
重新更新产品或服务
产品服务说明:
最近更新:
简要说明:
说明:
更新
@如果(ViewBag.Result==“成功”)
{
产品或服务
产品服务ID
产品服务说明
最后更新
简短描述
中等描述
@Prodservid模型
@Prodservdesc模型
@模型。上次更新
@Model.Serviceshortdesc
@Model.Servicedesc
}
这是控制器

using System;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PositionerWebConsumer.Models;

namespace PositionerWebConsumer.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public async Task<IActionResult> IndexAsync()
        {
            List<Prdsrv> PrdsrvList = new List<Prdsrv>();
            using (var httpClient = new System.Net.Http.HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44349/api/Prdsrvs"))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    PrdsrvList = JsonConvert.DeserializeObject<List<Prdsrv>>(apiResponse);
                }
            }
            return View(PrdsrvList);
        }

        public ViewResult GetProductService() => View();

        [HttpPost]
        public async Task<IActionResult> GetProductService(int id)
        {
           Prdsrv Prodserv = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44349/api/Prdsrvs/" + id))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    Prodserv= JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(Prodserv);
        }

        
        public async Task<IActionResult> UpdateProductService(int id)
        {
            Prdsrv Prodserv = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44349/api/Prdsrvs/" + id))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    Prodserv = JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(Prodserv);
        }

        [HttpPost]
        public async Task<IActionResult> UpdateProductService(Prdsrv Prodserv)
        {
            Prdsrv receivedProductservice = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                var content = new MultipartFormDataContent();
                content.Add(new StringContent(Prodserv.Prodservid.ToString()), "Prodservid");
                content.Add(new StringContent(Prodserv.Prodservdesc), "Prodservdesc");
                content.Add(new StringContent(Prodserv.Lastupdated.ToString()), "Lastupdated");
                content.Add(new StringContent(Prodserv.Serviceshortdesc), "Serviceshortdesc");
                content.Add(new StringContent(Prodserv.Servicedesc), "Servicedesc");
                using (var response = await httpClient.PutAsync("https://localhost:44349/api/Prdsrvs", content))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    ViewBag.Result = "Success";
                    receivedProductservice = JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(receivedProductservice);
        }

        public ViewResult AddProductService() => View();

        [HttpPost]
        public async Task<IActionResult> AddProductService(Prdsrv prdsrv)
        {
            Prdsrv receivedProductservice = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                StringContent content = new StringContent(JsonConvert.SerializeObject(prdsrv), Encoding.UTF8, "application/json");

                using (var response = await httpClient.PostAsync("https://localhost:44349/api/Prdsrvs", content))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    receivedProductservice = JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(receivedProductservice);
        }


        [HttpPost]
        public async Task<IActionResult> DeleteProductService(int ProdServId)
        {
            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.DeleteAsync("https://localhost:44349/api/Prdsrvs/" + ProdServId))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                }
            }

            return RedirectToAction("Index");
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
使用系统;
使用Microsoft.AspNetCore.Http;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
使用System.Net.Http;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用定位器WebConsumer.Models;
命名空间定位器WebConsumer.Controllers
{
公共类HomeController:控制器
{
专用只读ILogger\u记录器;
公共家庭控制器(ILogger记录器)
{
_记录器=记录器;
}
公共异步任务IndexAsync()
{
List PrdsrvList=新列表();
使用(var httpClient=new System.Net.Http.httpClient())
{
使用(var response=wait-httpClient.GetAsync(“https://localhost:44349/api/Prdsrvs"))
{
字符串apiResponse=wait response.Content.ReadAsStringAsync();
PrdsrvList=JsonConvert.DeserializeObject(apiResponse);
}
}
返回视图(PrdsrvList);
}
public ViewResult GetProductService()=>View();
[HttpPost]
公共异步任务GetProductService(int id)
{
Prdsrv Prodserv=新的Prdsrv();
使用(var httpClient=new httpClient())
{
使用(var response=wait-httpClient.GetAsync(“https://localhost:44349/api/Prdsrvs/“+id))
{
字符串apiResponse=wait response.Content.ReadAsStringAsync();
Prodserv=JsonConvert.DeserializeObject(apiResponse);
}
}
返回视图(Prodserv);
}
公共异步任务UpdateProductService(int id)
{
Prdsrv Prodserv=新的Prdsrv();
使用(var httpClient=new httpClient())
{
使用(var response=wait-httpClient.GetAsync(“https://localhost:44349/api/Prdsrvs/“+id))
{
字符串apiResponse=wait response.Content.ReadAsStringAsync();
Prodserv=JsonConvert.DeserializeObject(apiResponse);
}
}
返回视图(Prodserv);
}
[HttpPost]
公共异步任务UpdateProductService(Prdsrv Prodserv)
{
Prdsrv receivedProductservice=新Prdsrv();
使用(var httpClient=new httpClient())
{
var content=新的MultipartFormDataContent();
添加(新的StringContent(Prodserv.Prodservid.ToString()),“Prodservid”);
添加(新的StringContent(Prodserv.Prodservdesc),“Prodservdesc”);
Add(新的StringContent(Prodserv.Lastupdated.ToString()),“Lastupdated”);
添加(新的StringContent(Prodserv.Serviceshortdesc),“Serviceshortdesc”);
添加(新的StringContent(Prodserv.Servicedesc),“Servicedesc”);
使用(var response=wait-httpClient.PutAsync(“https://localhost:44349/api/Prdsrvs“,内容))
{
字符串apiResponse=wait response.Content.ReadAsStringAsync();
ViewBag.Result=“成功”;
receivedProductservice=JsonConvert.DeserializeObject(apiResponse);
}
}
返回视图(receivedProductservice);
@model Prdsrv
@{ Layout = "_Layout"; ViewBag.Title = "Update a Product Service";}

<h2>Update A Product or Service <a asp-action="Index" class="btn btn-sm btn-secondary">Back</a></h2>
<form asp-action="UpdateProductService" method="post">
    <div class="form-group">
        <label asp-for="Prodservid"></label>
        <input class="form-control" asp-for="Prodservid" readonly />
    </div>
    <div class="form-group">
        <label asp-for="Prodservdesc">Product Service Description:</label>
        <input class="form-control" asp-for="Prodservdesc" />
    </div>
    <div class="form-group">
        <label asp-for="Lastupdated">Last Updated:</label>
        <input class="form-control" asp-for="Lastupdated" />
    </div>
    <div class="form-group">
        <label asp-for="Serviceshortdesc">Short Description:</label>
        <input class="form-control" asp-for="Serviceshortdesc" />
    </div>
    <div class="form-group">
        <label asp-for="Servicedesc">Description:</label>
        <input class="form-control" asp-for="Servicedesc" />
    </div>
    <div class="text-center panel-body">
        <button type="submit" class="btn btn-sm btn-primary">Update</button>
    </div>
</form>

@if (ViewBag.Result == "Success")
{
    <h2>Product Or Service</h2>
    <table class="table table-sm table-striped table-bordered m-2">
        <thead>
            <tr>
                <th>Product Service ID</th>
                <th>Product Service Description</th>
                <th>Last Updated</th>
                <th>Short Description</th>
                <th>Medium Description</th>

            </tr>
        </thead>
        <tbody>
            <tr>
                <td>@Model.Prodservid</td>
                <td>@Model.Prodservdesc</td>
                <td>@Model.Lastupdated</td>
                <td>@Model.Serviceshortdesc</td>
                <td>@Model.Servicedesc</td>
            </tr>
        </tbody>
    </table>
}
using System;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PositionerWebConsumer.Models;

namespace PositionerWebConsumer.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public async Task<IActionResult> IndexAsync()
        {
            List<Prdsrv> PrdsrvList = new List<Prdsrv>();
            using (var httpClient = new System.Net.Http.HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44349/api/Prdsrvs"))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    PrdsrvList = JsonConvert.DeserializeObject<List<Prdsrv>>(apiResponse);
                }
            }
            return View(PrdsrvList);
        }

        public ViewResult GetProductService() => View();

        [HttpPost]
        public async Task<IActionResult> GetProductService(int id)
        {
           Prdsrv Prodserv = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44349/api/Prdsrvs/" + id))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    Prodserv= JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(Prodserv);
        }

        
        public async Task<IActionResult> UpdateProductService(int id)
        {
            Prdsrv Prodserv = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync("https://localhost:44349/api/Prdsrvs/" + id))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    Prodserv = JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(Prodserv);
        }

        [HttpPost]
        public async Task<IActionResult> UpdateProductService(Prdsrv Prodserv)
        {
            Prdsrv receivedProductservice = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                var content = new MultipartFormDataContent();
                content.Add(new StringContent(Prodserv.Prodservid.ToString()), "Prodservid");
                content.Add(new StringContent(Prodserv.Prodservdesc), "Prodservdesc");
                content.Add(new StringContent(Prodserv.Lastupdated.ToString()), "Lastupdated");
                content.Add(new StringContent(Prodserv.Serviceshortdesc), "Serviceshortdesc");
                content.Add(new StringContent(Prodserv.Servicedesc), "Servicedesc");
                using (var response = await httpClient.PutAsync("https://localhost:44349/api/Prdsrvs", content))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    ViewBag.Result = "Success";
                    receivedProductservice = JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(receivedProductservice);
        }

        public ViewResult AddProductService() => View();

        [HttpPost]
        public async Task<IActionResult> AddProductService(Prdsrv prdsrv)
        {
            Prdsrv receivedProductservice = new Prdsrv();
            using (var httpClient = new HttpClient())
            {
                StringContent content = new StringContent(JsonConvert.SerializeObject(prdsrv), Encoding.UTF8, "application/json");

                using (var response = await httpClient.PostAsync("https://localhost:44349/api/Prdsrvs", content))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    receivedProductservice = JsonConvert.DeserializeObject<Prdsrv>(apiResponse);
                }
            }
            return View(receivedProductservice);
        }


        [HttpPost]
        public async Task<IActionResult> DeleteProductService(int ProdServId)
        {
            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.DeleteAsync("https://localhost:44349/api/Prdsrvs/" + ProdServId))
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                }
            }

            return RedirectToAction("Index");
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
@page "/addprdsrv"
@using ThePositionerBlazorApp.Shared

@inject HttpClient Http
@inject NavigationManager NavigationManager

<h2>Create Product Service</h2>
<hr />
<form>
    <div class="row">
        <div class="col-md-8">
            <div class="form-group">
                <label for="Prodservid" class="control-label"> Product Service Id</label>
                <input form="Prodservid" class="form-control" @bind="@Prd.Prodservid" />
            </div>
            <div class="form-group">
                <label for="Prodservdesc" class="control-label"> Product Service Description</label>
                <input form="Prodservdesc" class="form-control" @bind="@Prd.Prodservdesc" />
            </div>
            <div class="form-group">
                <label for="Lastupdated" class="control-label"> Last Updated</label>
                <input form="Lastupdated" class="form-control" @bind="@Prd.Lastupdated" />
            </div>
            <div class="form-group">
                <label for="Serviceshortdesc" class="control-label"> Short Description</label>
                <input form="Serviceshortdesc" class="form-control" @bind="@Prd.Serviceshortdesc" />
            </div>
            <div class="form-group">
                <label for="Servicedesc" class="control-label"> Description</label>
                <input form="Servicedesc" class="form-control" @bind="@Prd.Servicedesc" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <input type="button" class="btn btn-primary" @onclick="@CreateProductService" value="Save" />
                <input type="button" class="btn" @onclick="@Cancel" value="Cancel" />
            </div>
         </div>
    </div>
</form>

@code {
    Prdsrv Prd = new Prdsrv();
    protected async Task CreateProductService()
    {
        await Http.PostJsonAsync("https://localhost:44349/api/Prdsrvs", Prd);
        NavigationManager.NavigateTo("ProductServiceList");
    }

    void Cancel()
    {
        NavigationManager.NavigateTo("ProductServiceList");
    }

}
@page "/editprdsrv/{id}"
@using ThePositionerBlazorApp.Shared

@inject HttpClient Http
@inject NavigationManager NavigationManager

<h2>Create Product Service</h2>
<hr />
<form>
    <div class="row">
        <div class="col-md-8">
            <div class="form-group">
                <label for="Prodservid" class="control-label"> Product Service Id</label>
                <input form="Prodservid" class="form-control" @bind="@Prd.Prodservid" />
            </div>
            <div class="form-group">
                <label for="Prodservdesc" class="control-label"> Product Service Description</label>
                <input form="Prodservdesc" class="form-control" @bind="@Prd.Prodservdesc" />
            </div>
            <div class="form-group">
                <label for="Lastupdated" class="control-label"> Last Updated</label>
                <input form="Lastupdated" class="form-control" @bind="@Prd.Lastupdated" />
            </div>
            <div class="form-group">
                <label for="Serviceshortdesc" class="control-label"> Short Description</label>
                <input form="Serviceshortdesc" class="form-control" @bind="@Prd.Serviceshortdesc" />
            </div>
            <div class="form-group">
                <label for="Servicedesc" class="control-label"> Description</label>
                <input form="Servicedesc" class="form-control" @bind="@Prd.Servicedesc" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4">
            <div class="form-group">
                <input type="button" class="btn btn-primary" @onclick="@UpdateProductService" value="Update" />
                <input type="button" class="btn" @onclick="@Cancel" value="Cancel" />
            </div>
        </div>
    </div>
</form>

@code {
    [Parameter]
    public string id { get; set; }

    Prdsrv Prd = new Prdsrv();


    protected override async Task OnInitializedAsync()

    {
        Prd = await Http.GetJsonAsync<Prdsrv>("https://localhost:44349/api/Prdsrvs/" + id);
    }

    protected async Task UpdateProductService()
    {
        await Http.PutJsonAsync("https://localhost:44349/api/Prdsrvs", Prd);
        NavigationManager.NavigateTo("ProductServiceList");
    }

    void Cancel()
    {
        NavigationManager.NavigateTo("ProductServiceList");
    }


}