Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core default route as /Home with it shown by default

Hi I use the mapRoutes in ASP.Net Core I can set the default controller route. When starting the application the entry point will be the default Home but all that will be shown is https://localhost:43594 rather than showing the /Home on the end.

Is there a way to explicitly enforce this?

like image 557
Sigex Avatar asked Oct 22 '25 01:10

Sigex


2 Answers

There are several workarounds:

  • Workaround 1: you can remove the defaults as described here
  • Workaround 2: create a different default action and redirect to /Home/Index from there as described here
  • Workaround 3: check for current URL and redirect if it is root as described here
like image 100
René Avatar answered Oct 24 '25 21:10

René


For changing Route URL, you could try URL Rewriting Middleware or Custom Middleware.

Here are available options in Startup:

  • Rewrite URL

     app.UseRewriter(new RewriteOptions().AddRewrite("/","/Home",true));
    
  • Redirect URL

     app.UseRewriter(new RewriteOptions().AddRedirect("/", "/Home"));
    
  • Custom Middleware

        app.Use(async (context, next) =>
        {
            if (context.Request.Path == "/")
            {
                context.Response.Redirect("Home/", true);
    
                return;
            }
            await next();
        });
    

    Use the above options before app.UseMvc

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    
like image 36
Edward Avatar answered Oct 24 '25 20:10

Edward