M
M
mr_blond972019-09-02 23:16:10
ASP.NET
mr_blond97, 2019-09-02 23:16:10

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();
            }
        }

When url 0.0.0.0:5000/api/main is opened, the ArticlesController.Index() method is called.
You need to make a route that will call the same method when opening the url http://0.0.0.0:5000.
Startup.cs class:
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"});
            });
        }
    }
}

When opening url 0.0.0.0:5000 this method is not called.
How to make such a router?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question