0

In MVC5 you could set the default route using the following attribute on a controller?

[Route("{action=index}")]

What is the equivalent of this in MVC6?

Update

This is the code I had in MVC5

    [Route("{action=index}")]
    public class StaticPagesController : Controller
    {
        public ActionResult About()
        {
           return View();
        }

        public ActionResult Contact()
        {
           return View();
        }

        public ActionResult Index()
        {
           return View();
        }
    }

I have not been able to work out how to do the equivalent in MVC6 but I've been able to get the same functionality working using the following:

    [Route("[action]")]
    public class StaticPagesController : Controller
    {
        public ActionResult About()
        {
           return View();
        }

        public ActionResult Contact()
        {
           return View();
        }

        [Route("/[action]")]
        [Route("/")]
        public ActionResult Index()
        {
           return View();
        }
    }
Cool Breeze
  • 1,289
  • 11
  • 34

1 Answers1

1

You can decorate your specific action method which you want to be the default action with Route attribute and pass [controller] as the route template to that. So anytime you get a request for yoursite/yourcontroller, the incoming request will be redirected to this specific action method.

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

    [Route("[controller]")]
    public IActionResult OtherIndex()
    {
        return Content("This will be the response for mySite/Settings request");
    }
}

Edit : As per the comment

I don't want to include the controller name in the URL.I want it to be domain.com/About rather than domain.com/StaticPages/About

Using attribute routing, you can decorate your action method with the Route attribute and give [action] as the template name.

public class StaticPagesController : Controller
{      
    [Route("[action]")]
    public IActionResult About()
    {
        // This action method will be executed for "yourSite/About" request
        return View();
    }
}

With the above approach, you cannot have 2 action method's with the same name in your app ( Ex : You cannot have an About action method in HomeController and StaticPagesController)

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • [controller] on action? – Cool Breeze Jan 23 '16 at 04:47
  • @CoolBreeze yes. To make that action method the default one for the controller. – Shyju Jan 23 '16 at 15:38
  • This did not work. I am not sure if it's because I have the following attribute on my controller: [Route("[action]")] public class StaticPagesController : Controller – Cool Breeze Jan 25 '16 at 00:34
  • Yes. You don't need that controller level attribute. Try the code I have in the answer. It should work fine. – Shyju Jan 25 '16 at 00:50
  • I do because I don't want to include the controller name in the URL. e.g. I want it to be http://domain.com/About rather than http://domain.com/StaticPages/About – Cool Breeze Jan 25 '16 at 05:14
  • Unfortunately this does not produce the desired behavior. Please see the update to my post for more information. I've got it working so it's no longer a big deal but still feels a little bit redundant... – Cool Breeze Jan 26 '16 at 01:47
  • It does. With the second update when you access yourSitename/About , It goes About of StatucPagesController. I am not sure why you think it is not the desired behavior ? – Shyju Jan 26 '16 at 02:02
  • Because when someone visits yourSitename.com it should navigate to the Index view. – Cool Breeze Jan 26 '16 at 02:12
  • It should work fine unless you have some custom route alteration. – Shyju Jan 26 '16 at 02:40
  • Couldn't get it to work. Oh well ended up using what was pasted in my question. – Cool Breeze Feb 01 '16 at 10:08
  • It should work. I created a minimal project and verified it, If you do not trust me, download it [here](https://onedrive.live.com/redir?resid=B9ABE38A81F442D6!3069&authkey=!AHyT8p2z_MaeVtg&ithint=file%2czip) and see yourself. – Shyju Feb 03 '16 at 01:45
  • Your sample includes convention based routing in Startup.cs. I think that is why your example is working but not mine. – Cool Breeze Feb 15 '16 at 03:12