Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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
在我的Blazor项目中从未调用API控制器的构造函数_Blazor_Blazor Server Side_Blazor Client Side_Asp.net Blazor - Fatal编程技术网

在我的Blazor项目中从未调用API控制器的构造函数

在我的Blazor项目中从未调用API控制器的构造函数,blazor,blazor-server-side,blazor-client-side,asp.net-blazor,Blazor,Blazor Server Side,Blazor Client Side,Asp.net Blazor,我是Blazor的新手,我创建了一个API控制器来处理我的CRUD例程。它有一个构造函数,因此我可以将与数据库的连接保存到私有成员。然而,当我的应用程序启动时,构造函数从未被调用。我还试图向控制器发送一个帖子,我收到了一个错误的请求,所以我假设它仍然没有正确设置。谢谢你的帮助 StartUp.cs public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDe

我是Blazor的新手,我创建了一个API控制器来处理我的CRUD例程。它有一个构造函数,因此我可以将与数据库的连接保存到私有成员。然而,当我的应用程序启动时,构造函数从未被调用。我还试图向控制器发送一个帖子,我收到了一个错误的请求,所以我假设它仍然没有正确设置。谢谢你的帮助

StartUp.cs

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
      }
      else
      {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios,
        // see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
      }

      app.UseHttpsRedirection();
      app.UseStaticFiles();

      app.UseRouting();

      app.UseAuthentication();
      app.UseAuthorization();

      app.UseEndpoints(endpoints =>
      {
        endpoints.MapControllers();
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
      });

    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ComEd.PTS.DataStore.Models;
using Microsoft.AspNetCore.Mvc;

namespace ComEd.PTS.Event.UI.Controllers
{
  [Route("api/[controller]")]
  public class PTSEventController : ControllerBase
  {
    private readonly PeakTimeSavingsDBContext _dbContext;

    public PTSEventController(PeakTimeSavingsDBContext dbContext)
    {
      _dbContext = dbContext; //<<<< The constructor never gets called >>>>
    }

    // GET: api/<controller>
    [HttpGet]
    public IEnumerable<string> Get()
    {
      return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
      return "value";
    }

    // POST api/<controller>
    [HttpPost]
    public void Post([FromBody]string value)
    {
      int x = 0;
      x++;
    }

    // PUT api/<controller>/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody]string value)
    {
      int x = 0;
      x++;

    }

    // DELETE api/<controller>/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
      int x = 0;
      x++;
    }
  }
}
PTSEventController.cs

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
      }
      else
      {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios,
        // see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
      }

      app.UseHttpsRedirection();
      app.UseStaticFiles();

      app.UseRouting();

      app.UseAuthentication();
      app.UseAuthorization();

      app.UseEndpoints(endpoints =>
      {
        endpoints.MapControllers();
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
      });

    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ComEd.PTS.DataStore.Models;
using Microsoft.AspNetCore.Mvc;

namespace ComEd.PTS.Event.UI.Controllers
{
  [Route("api/[controller]")]
  public class PTSEventController : ControllerBase
  {
    private readonly PeakTimeSavingsDBContext _dbContext;

    public PTSEventController(PeakTimeSavingsDBContext dbContext)
    {
      _dbContext = dbContext; //<<<< The constructor never gets called >>>>
    }

    // GET: api/<controller>
    [HttpGet]
    public IEnumerable<string> Get()
    {
      return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
      return "value";
    }

    // POST api/<controller>
    [HttpPost]
    public void Post([FromBody]string value)
    {
      int x = 0;
      x++;
    }

    // PUT api/<controller>/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody]string value)
    {
      int x = 0;
      x++;

    }

    // DELETE api/<controller>/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
      int x = 0;
      x++;
    }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用comet.PTS.DataStore.Models;
使用Microsoft.AspNetCore.Mvc;
命名空间comet.PTS.Event.UI.Controllers
{
[路由(“api/[控制器]”)]
公共类PTSEventController:ControllerBase
{
私有只读PeakTimeSavingsDBContext_dbContext;
公共PTSEventController(PeakTimeSavingsDBContext dbContext)
{
_dbContext=dbContext;/>
}
//获取:api/
[HttpGet]
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}
//获取api//5
[HttpGet(“{id}”)]
公共字符串Get(int-id)
{
返回“值”;
}
//后api/
[HttpPost]
公共作废帖子([FromBody]字符串值)
{
int x=0;
x++;
}
//放置api//5
[HttpPut(“{id}”)]
公共void Put(int id,[FromBody]字符串值)
{
int x=0;
x++;
}
//删除api//5
[HttpDelete(“{id}”)]
公共无效删除(int-id)
{
int x=0;
x++;
}
}
}

是否将您的类添加为服务

在startup.cs中的ConfigureServices()中:

services.AddSingleton<PTSEventController>();
services.AddSingleton();

我猜您的控制器不在Web Api中,对吗?我建议您在服务器端blazor所在的解决方案中创建一个Web Api项目。或者创建一个服务。请发布ConfigureServices方法的内容。我们可以看到尝试调用“api/PTSEvent”的.razor代码吗?嘿,我让它工作了。我假设当应用程序启动时,控制器的构造函数会被调用,但它没有。它不会发生,直到你得到或张贴到它。我的问题是我没有为控制器使用正确的URI。正确的URL为,而不是。我的错。谢谢大家的帮助。经典(URL错误);)向项目中添加Swagger通常是一个好主意-然后您可以浏览API以及如何在控制器等上调用不同函数的易于使用的示例。学习需要一点时间,但(我认为)非常值得花时间/精力