我試圖在我的 Blazor WASM 專案中實作一個簡單的 MVC 控制器,但我無法讓路由正常作業。當我嘗試訪問它時,它總是將我重定向到 blazor“NotFound”組件。我花了很多時間嘗試在我的 Startup.cs 中進行配置,但我的想法已經用完了。我正在.NET6 中的樣板 WASM 專案上執行此操作。這就是我的 Startup.cs 的樣子:
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureApplicationServices();
services.ConfigurePersistenceServices(Configuration);
//services.AddMvc();
services.AddControllersWithViews();
services.AddRazorPages();
}
// 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.UseMigrationsEndPoint();
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.UseMiddleware<ExceptionMiddleware>();
app.UseCors(config =>
config
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
//.WithExposedHeaders("header1", "header")
);
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapDefaultControllerRoute();
endpoints.MapRazorPages();
endpoints.MapFallbackToFile("index.html");
});
}
這是我的 MVC 控制器:
[Route("[controller]/[action]")]
public class RoleManagerController : Controller
{
private readonly RoleManager<IdentityRole> _roleManager;
public RoleManagerController(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
public async Task<IActionResult> Index()
{
var roles = await _roleManager.Roles.ToListAsync();
return View(roles);
}
[HttpPost]
public async Task<IActionResult> AddRole(string roleName)
{
if (roleName != null)
{
await _roleManager.CreateAsync(new IdentityRole(roleName.Trim()));
}
return RedirectToAction("Index");
}
}
uj5u.com熱心網友回復:
[禮貌] 你的問題中一定有一些遺漏的資訊。
我已經使用 Net6.0 開箱即用的 Blazor Hosted 模板對此進行了測驗,并且可以正常作業。
這是我的控制器:
[ApiController]
[Route("[controller]/[action]")]
public class MyController : ControllerBase
{
public string Index()
{
return "hello. You called Index";
}
}
我的(開箱即用)Program:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
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.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
郵遞員結果(使用小寫字母):

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/404846.html
標籤:
下一篇:發布時排除檔案夾
