我在 asp net core web 应用程序项目中使用 Orchard core。我有一个带有两个简单的 get 和 post API 的控制器。由于我使用 OrchardCore,Startup.cs 文件具有不同的配置,并且我不在 configureServices 中使用 services.AddControllers() 。 一切都很好,直到我使用 HttpGet。但是当我想要一个带有 HttpPost 的 Api 时,postMan 说 badRequest。所以我在 Startup.cs 中添加了 services.AddControllers() ,并且 post Api 在 post Man 中很好,但果园项目说我有多个端点。 我使用 services.AddMvc().AddNewtonsoftJson(),一切都很好,但管理页面未加载并出现如下错误:
InvalidOperationException:未找到视图“索引”。这 搜查了以下地点: /Areas/OrchardCore.AdminDashboard/Views/Dashboard/Index.cshtml /Areas/OrchardCore.AdminDashboard/Views/Shared/Index.cshtml /Views/Shared/Index.cshtml /Pages/Shared/Index.cshtml
如果您能帮助我如何调用 Post Api,我将不胜感激。 这是我的代码:
[HttpPost("post")]
public Task<string> post()
{
return Task.FromResult("hiPost");
}
[HttpGet("get")]
public Task<string> get()
{
return Task.FromResult("hiGet");
}
这是我的startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
//services.AddControllers();
services.AddOrchardCms();
services.AddMediatR(typeof(SelectedWebSiteBlogQueryHandler).Assembly);
services.AddAutoMapper(typeof(Startup));
services.AddCors();
services.AddMvc().AddNewtonsoftJson();
}
public void Configure(IApplicationBuilder app, IHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(o => o.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseOrchardCore();
}
}
您的控制器上可能缺少
IgnoreAntiForgeryToken
属性。
AntiForgery
默认情况下由 OrchardCore
启用
对于
ApiController
中的 OrchardCore
,我希望看到控制器装饰如下。
[ApiController]
[Authorize(AuthenticationSchemes = "Api"), IgnoreAntiforgeryToken, AllowAnonymous]
但是,这取决于您是否使用
OpenId
模块进行身份验证,或者只是需要发布到普通控制器,而不需要 AuthenticationScheme
根据您在现实生活中实际发布的内容,最好在帖子中提供防伪令牌。