首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >提供了一个无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress。在巴兹尔

提供了一个无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress。在巴兹尔
EN

Stack Overflow用户
提问于 2019-10-10 06:31:12
回答 2查看 4K关注 0票数 2

碰到控制器异常错误时,“提供了无效的请求URI。请求URI必须是绝对URI,或者必须设置BaseAddress。”*强文本

blazor服务器端

控制器调用在Razor组件视图代码中不执行

代码语言:javascript
复制
  async Task UploadFile()
  {
    try
    {
      LoginRepository loginRepository = new LoginRepository(new LaborgDbContext());
      DocumentService documentService = new DocumentService();
      var form = new MultipartFormDataContent();
      var content = new StreamContent(file.Data);
      content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
      {
        Name = "files",
        FileName = file.Name
      };
      form.Add(content);
      var response = await HttpClient.PostAsync("/api/Document/Upload", form);    
    }
    catch (Exception ex)
    {
      throw ex;
    }

  }

控制器码

代码语言:javascript
复制
[Route("api/[controller]/[action]")]
  [ApiController]
  public class UploadController : ControllerBase
  {
    private readonly IWebHostEnvironment _Env;

    public UploadController(IWebHostEnvironment env)
    {
      _Env = env;
    }
    [HttpPost()]
    public async Task<IActionResult> Post(List<IFormFile> files)
    {
      long size = files.Sum(f => f.Length);
      foreach (var formFile in files)
      {
        // full path to file in temp location
        var filePath = Path.GetTempFileName();
        if (formFile.Length > 0)
        {
          using (var stream = new FileStream(filePath, FileMode.Create))
          {
           await formFile.CopyToAsync(stream);
          }
        }

        System.IO.File.Copy(filePath, Path.Combine(_Env.ContentRootPath, "Uploaded", formFile.FileName)); 
      }

      return Ok(new { count = files.Count, size });
    }

启动

代码语言:javascript
复制
  public class Startup

{公共启动(IConfiguration配置){ Configuration = configuration;}

代码语言:javascript
复制
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
  services.AddAuthorizationCore();
  services.AddDbContext<ApplicationDbContext>(options =>
      options.UseSqlServer(
          Configuration.GetConnectionString("DefaultConnection")));
  services.AddDefaultIdentity<IdentityUser>()
      .AddEntityFrameworkStores<ApplicationDbContext>();
  services.AddRazorPages();
  services.AddServerSideBlazor();
  services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
  services.AddSingleton<WeatherForecastService>();
  services.AddSingleton<TaskSchedulerService>();
  services.AddSingleton<TimeOffSchedulerService>();
  services.AddSingleton<DocumentService>();
  services.AddFileReaderService(options => options.InitializeOnFirstCall = true);
  services.AddSingleton<HttpClient>();

  services
.AddBlazorise(options =>
{
  options.ChangeTextOnKeyPress = true; // optional
})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  if (env.IsDevelopment())
  {
    app.UseDeveloperExceptionPage();
    app.UseDatabaseErrorPage();
  }
  else
  {
    app.UseExceptionHandler("/Error");

    app.UseHsts();
  }

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

  app.UseRouting();
  app.UseEmbeddedBlazorContent(typeof(MatBlazor.BaseMatComponent).Assembly);
  app.UseEmbeddedBlazorContent(typeof(BlazorDateRangePicker.DateRangePicker).Assembly);
  app.UseAuthentication();
  app.UseAuthorization();

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

}}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-10 13:28:06

startup.cs页面并将以下代码添加到app.UseEndpoints方法的末尾(在endpoints.MapFallbackToPage("/_Host");行下),以允许正确地路由到控制器的http请求。

添加以下行endpoints.MapControllerRoute("default","{controller=Home}/{action=Index}/{id?}");

代码语言:javascript
复制
app.UseEndpoints(endpoints =>
      {
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
        endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
      });
票数 1
EN

Stack Overflow用户

发布于 2020-08-03 19:03:06

https://github.com/dotnet/aspnetcore/issues/16840

当位置包含%20时,Blazor抛出

出发地:@msftbot

我们已经将这个问题转移到了待办事项的里程碑上。这意味着它不会在即将发布的版本中使用。我们将在当前发布后重新评估待办事项,并在那时审议此项目。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58316755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档