Answer the question
In order to leave comments, you need to log in
How to make a home/index route that will call another controller's method?
Controller with Index() method:
[Produces("application/json")]
[Route("api/Articles")]
public class ArticlesController : Controller
{
public ArticlesController()
{
}
public IActionResult Index()
{
return View();
}
}
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"});
});
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question