简体   繁体   English

如何为MVC CORE项目制作真正的默认路由?

[英]How do I make a true default route for a MVC CORE project?

I have the following setup for my HomeController. 我的HomeController具有以下设置。 I want all my MVC controllers to respond from /mvc/[controler]/[action] and the WebApi ones on /api/ 我希望我所有的MVC控制器都能从/ mvc / [controler] / [action]和/ api /上的WebApi做出响应

But when someone goes to the no-path version of the host, I want it to go to /mvc/home/index, my setup doesn't seem to do that! 但是,当有人使用主机的无路径版本时,我希望它进入/ mvc / home / index,我的设置似乎没有做到这一点!

I want http://hostname/ to got to http:/hostname/Home/Index 我希望http:// hostname /进入http:/ hostname / Home / Index

    [Route("mvc/[controller]")]
[ApiExplorerSettings(IgnoreApi = true)]
public class HomeController : Controller
{
    [Route("Index")] 
    public IActionResult Index()
    {
        return View();
    }

    [Route("Error")]
    public IActionResult Error()
    {
        return View();
    }
}

my startup.cs: 我的startup.cs:

      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();
        app.UseMiddleware(typeof(ErrorHandlingMiddleware));
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                "default",
                "mvc/{controller=Home}/{action=Index}/{id?}");
        });

        app.UseSwagger();

        app.UseSwaggerUI(c =>
        {
            var swaggerendpoint = Configuration["SwaggerEndpoint"];
            c.SwaggerEndpoint(swaggerendpoint, "Public Cloud Interface");
        });
    }
}

Add a route specifically for the / case: 添加专门针对/ case的路由:

app.UseMvc(routes =>
    {
        routes.MapRoute(
            "home",
            "/", 
            new{controller="Home", Action="Index"});

        routes.MapRoute(
            "default",
            "mvc/{controller=Home}/{action=Index}/{id?}");
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM