Контроллер с Index() методом:
[Produces("application/json")]
[Route("api/Articles")]
public class ArticlesController : Controller
{
public ArticlesController()
{
}
public IActionResult Index()
{
return View();
}
}
При открытии url
0.0.0.0:5000/api/main вызывается метод ArticlesController.Index().
Нужно сделать route, который будет вызывать тот же метод при открытии url
http://0.0.0.0:5000.
Класс Startup.cs:
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
namespace AppName
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration.GetConnectionString("base");
services.AddDbContext<AnalysisContext>(options =>
options.UseMySQL(connection));
services.AddMvc();
/* DI */
new StartupDi().ConfigureServices(services);
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
}
//// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404
&& !Path.HasExtension(context.Request.Path.Value)
&& !context.Request.Path.Value.Contains("api/")
)
{
context.Request.Path = "/index.html";
await next();
}
});
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseCors("MyPolicy");
app.UseMvc(routes =>
{
routes.MapRoute("default1", "articles{controller}/{action?}/{id?}");
routes.MapRoute("default2", "", defaults: new {controller = "Articles", action = "Index"});
routes.MapRoute("default3", "Store/Buy", defaults: new {controller = "Articles", action = "Index"});
});
}
}
}
При открытии url
0.0.0.0:5000 данный метод не вызывается.
Как сделать такой router?