6

I can't seem to get my default route working in ASP.NET Core 2.0, am I overlooking something ?

Startup.cs

public class Startup
{
    public IConfiguration Configuration { get; set; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

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

HomeController.cs

[Route("[controller]")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

when I browse to the URL, nothing happens, and it does not redirect to Home ?

4
  • what url are you trying ? Commented Sep 29, 2017 at 13:49
  • localhost:5000 Commented Sep 29, 2017 at 13:50
  • when I go to localhost:5000/Home, it shows the page. It just doesn't take it as default ... Commented Sep 29, 2017 at 13:51
  • Don't you need to define "template"? Just asking. Commented Sep 29, 2017 at 13:52

2 Answers 2

13

Just remove the [Route("[controller]")] decoration on the controller.

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

With the default routing you registered in the UseMvc method with the conventional route patterns, now it should work for yourBaseUrl and yourBaseUrl\Home and yourBaseUrl\Home\Index

Typically you use the [Route("[controller]")] attribute on a controller level as a route prefix for all the routes on that controller to create custom attribute route definitions for your action methods.

[Route("[controller]")]
public class HomeController : Controller
{
    [Route("myseofriendlyurlslug")]
    public IActionResult Index()
    {
        return View();
    }
}

Now your action method will be accessible via yourBaseUrl/Home/myseofriendlyurlslug

Keep in mind that, when using attribute routing like above, the conventional routing pattern won't work.

Sign up to request clarification or add additional context in comments.

1 Comment

I ran into this too, makes sense now that I think about it. Attributes should prioritize over default routing templates.
2

this work fine

//Startup.cs

...

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

...

//HomeController.cs

...

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.