【发布时间】:2021-02-10 07:20:10
【问题描述】:
我使用模板 Blazor Server、Webassembly、Shared 构建 Blazor。它工作正常。
但是当我尝试使用邮递员进行测试时。它总是显示这个
以下是我的测试控制器。我可以使用 blazor httpclient 访问休息。但我想用 postmna 测试
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SchoolHero.Server.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SchoolHero.Server.Controllers.V1
{
[ApiVersion("1.0")]
[AllowAnonymous]
public class BuggyController : BaseApiController
{
private readonly ApplicationDbContext _context;
public BuggyController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet("testauth")]
[Authorize]
public ActionResult<string> GetSecretStuff()
{
return "super secret";
}
[HttpGet("notfound")]
public ActionResult GetNotFoundRequest()
{
var thing = _context.Developers.Find(46);
if (thing == null)
{
return NotFound();
}
return Ok();
}
[HttpGet("servererror")]
public ActionResult GetServerError()
{
var thing = _context.Developers.Find(46);
var thingToReturn = thing.ToString();
return Ok();
}
[HttpGet("badrequest")]
public ActionResult GetBadRequest()
{
return BadRequest();
}
[HttpGet("badrequest/{id}")]
public ActionResult GetNotFoundWithId(int id)
{
return BadRequest();
}
}
}
/// 这是启动类。我注册。看起来不错
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.ResponseCompression; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SchoolHero.Server.Extensions; using System.Linq; using System.Threading.Tasks;
namespace SchoolHero.Server {
public class Startup
{
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
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.AddContextInfrastructure(_configuration);
services.AddEssentials();
services.AddIdentityService(_configuration);
services.AddControllersWithViews();
services.AddRazorPages();
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebAssemblyDebugging();
}
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.UseCors("CorsPolicy");
app.ConfigureSwagger();
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.Map("api/{**slug}", HandleApiFallback);
endpoints.MapFallbackToFile("index.html");
});
}
private Task HandleApiFallback(HttpContext context)
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return Task.FromResult(0);
}
} }
【问题讨论】: