我試圖在我的控制器中使用Microsoft.AspNetCore.Mvc.ActionContext,但我得到了這個錯誤,即使我在MyController中做了DI。 我不知道如何解決它,請幫助
public class MyController : Controller
{
private readonly IWebHostEnvironment _hostEnvironment;
private readonly ActionContext _actionContext;
public StudentsController()
IWebHostEnvironment hostEnvironment。
ActionContext actionContext)。
{
_hostEnvironment = hostEnvironment。
_actionContext = actionContext。
}
在這里,我試圖在控制器方法中訪問ActionContext:
[HttpPost]
public async Task<IActionResult> RegistrationPdf()
{
string wwwRootPath = _hostEnvironment.WebRootPath;
ViewAsPdf pdf = new ViewAsPdf("RegistrationPdf")
{
FileName = "RegistrationPdf.pdf"。
};
byte[] pdfData = pdf.BuildFile(_actionContext).Result;
string fullPath = @"Files"/span> pdf.FileName;
using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write)
{
fileStream.Write(pdfData, 0, pdfData.Length)。
}
return RedirectToAction("Registration"/span>)。
}
StartUp.cs
namespace Student>
{
public class Startup {
{
public Startup(IConfiguration configuration)
{
配置 = 配置。
}
public IConfiguration 配置 { get; }
//此方法被運行時呼叫。使用此方法來向容器添加服務。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection") )。
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddDefaultUI()
.AddEntityFrameworkStores<ApplicationDbContext>()。
services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = true;
options.Lockout.AllowedForNewUsers = true;
});
services.ConfigureApplicationCookie(options =>
{
ptions.SlidingExpiration = true;
});
services.AddControllersWithViews()。
services.AddRazorPages(options =>
{
options.Convention.AuthorizePage("/Students/StudentInfo/"/span>)。
options.Convention.AuthorizeFolder("/Private"/span>)。
});
services.AddDbContext<TrainingDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection2") )。
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)。
});
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build()。
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage()。
app.UseDatabaseErrorPage();
}
else { env.IsDevelopment()
{
app.UseExceptionHandler("/Home/Error"/span>)。
app.UseHsts()。
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
名稱。"default"。
模式。"{controller=Home}/{action=Index}/{id?}")。)
endpoints.MapRazorPages()。
endpoints.MapHub<ChatHub>("/chatHub") 。
});
RotativaConfiguration.Setup((Microsoft.AspNetCore.Hosting.IHostingEnvironment)en)。
}
}
uj5u.com熱心網友回復:
改變你的代碼,如下:
private readonly Microsoft.AspNetCore.Mvc.Infrastructure.IActionContextAccessor actionContextAccessor;
public HomeController(Microsoft.AspNetCore. Mvc.Infrastructure.IActionContextAccessor actionContextAccessor,IConfiguration configuration,IWebHostEnvironment env, MvcCore3_1Context context)。
{
this.actionContextAccessor = actionContextAccessor。
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>()。
services.AddControllersWithViews()。
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection()。
app.UseStaticFiles();
RotativaConfiguration.Setup(env.WebRootPath, "Rotativa" )。
app.UseRouting()。
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
名稱。"default"。
模式。"{controller=Home}/{action=Privacy}/{id?}")。)
});
}
參考資料:
https://stackoverflow.com/a/61864127/11398810
uj5u.com熱心網友回復:
問題解決了。但我有點好奇。你的類名是MyController,但建構式是StudentsController。
public class MyController : Controller >。
{
private readonly IWebHostEnvironment _hostEnvironment;
private readonly ActionContext _actionContext;
public StudentsController()
IWebHostEnvironment hostEnvironment,
控制元件
onContext actionContext)。
{
_hostEnvironment = hostEnvironment。
_actionContext = actionContext。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/320335.html
標籤:
