VolcovMeter VolcovMeter - 1 month ago 12
C# Question

Hiding controller name from URL

This question is in regard to the newest stable release of ASP.Net Core.

Just like previously asked questions I've browsed on StackOverflow, what I'm trying to accomplish is, when visiting any other view other than

Index
in the
Home
Views folder, and for example, visiting
About
, the URL reading:

www.example.com/About


and not:
www.example.com/Home/About


My code:

In a newly started ASP.Net Core App, in the
Startup.cs
file, there's a Configure function:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}


Things I've tried:

I've tried changing the the routes to:

routes.MapRoute(
"Home",
"",
new { action = Index, controller = Home }
);


Didn't work. I've also tried inserting this:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"OnlyAction",
"{action}",
new { controller = "Home", action = "Index" }
);


But unfortunately, ASP.Net doesn't recognize the
IgnoreRoute
function yet.

Anyone else running into this with Core? Any help would be appreciated.

Answer

According to Asp.Net Core Routing documentation

Something like the following should allow for what you described

routes.MapRoute(
    name: "root",
    template: "{action}/{id?}",
    defaults: new { controller = "Home", action = "Index" });
Comments