I'm currently using ASP.Net Core MVC project, so I'm in my view route as:
https://localhost:4400/advertisers
That view's my index and I have advertisers controller with get and post method:
Get controller:
public async Task<IActionResult> Index()
{
var advertiserList = await _advertisersService.GetActiveAsync();
return View(advertiserList);
}
So, in my index view I have a a button "Create Advertiser" as:
<a asp-action="Create" asp-controller="Advertisers"><button type="button" ><i ></i> Add Advertiser</button></a>
The method on advertisers controller looks as:
[HttpGet]
public IActionResult Create()
{
return View();
}
But when I click the button it Chrome throws
This localhost page can’t be found No webpage was found for the web address: https://localhost:4400/advertisers/create HTTP ERROR 404
the route it is trying to access is:
https://localhost:4400/advertisers/create
Thanks in advance
Startup.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();
}
);
}
CodePudding user response:
Try to put the following code to the program.cs
, just for test. This is default conventional routing code and it's working for me, at least;
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();