访问控制器索引内的路由



我目前正在使用ASP。Net Core MVC项目,所以我的观点是:

https://localhost:4400/advertisers

这个视图是我的索引,我有一个广告商控制器,使用获取和发布方法:

获取控制器:

public async Task<IActionResult> Index()
{
var advertiserList = await _advertisersService.GetActiveAsync();
return View(advertiserList);
}

因此,在我的索引视图中,我有一个按钮";创建广告商";作为:

<a asp-action="Create" asp-controller="Advertisers"><button type="button" class="btn btn-primary waves-effect waves-primary"><i class="bx bx-plus me-1"></i> Add Advertiser</button></a>

广告商控制器上的方法看起来像:

[HttpGet]
public IActionResult Create()
{
return View();
}

但当我点击按钮时,Chrome会抛出

找不到此localhost页面未找到该网站的网页地址:https://localhost:4400/advertisers/createHTTP错误404

它试图访问的路由是:

https://localhost:4400/advertisers/create

提前感谢

启动.cs

public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting(options => options.LowercaseUrls = true);
services
.AddControllersWithViews()
.AddSessionStateTempDataProvider()
.AddRazorRuntimeCompilation();

[UsedImplicitly]
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IApiVersionDescriptionProvider provider,
ApplicationDbContext db
)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/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();
}
db.Database.Migrate();
app.UseSession();
app.UseSwagger();
app.UseSwaggerUI(
options =>
{
foreach (var groupName in provider.ApiVersionDescriptions.Select(x => x.GroupName))
options.SwaggerEndpoint($"/swagger/{groupName}/swagger.json", $"{groupName}");
}
);
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(
endpoints =>
{
endpoints.MapDefaultControllerRoute();
}
);
}

尝试将以下代码放入program.cs中,仅用于测试。这是默认的传统路由代码,至少对我有效;

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/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.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); });
app.Run();

相关内容

  • 没有找到相关文章

最新更新