mohamedelhawary
mohamedelhawary

Reputation: 113

set index action default action controller in asp.net mvc

I am trying to make index action is the default action in this controller when i write

http//mydomain/action

call index action > http//mydomain/case-studies

will call index action

my problem when i write the url gives me this message The resource cannot be found. the url i write it http//mydomain/case-studiesit should be call index action but The resource cannot be found.

This is the controller

 [RoutePrefix("case-studies")]
public class case_studiesController : Controller
{
    // GET: CaseStudies
    [Route("Index")]
    public ActionResult Index()
    {

        return View("/views/case-studies/Index.cshtml");
    }

    [Route("adaep")]
    public ActionResult adaep()
    {
        return View("/views/case-studies/pagename.cshtml");
    }
}

this is my route

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

    }
}

Upvotes: 0

Views: 3663

Answers (2)

mohamedelhawary
mohamedelhawary

Reputation: 113

This answer worked with me to keep home route as default and make default action for every controller i changed this from [Route("Index")] to [Route("")] with the same route

      [Route("")]
    public ActionResult Index()
    {

        return View("/views/case-studies/Index.cshtml");
    }

 public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id =    UrlParameter.Optional }
    );

   }
}

That route does that.the definition of the route is: if {controller} is empty use "Home" if {action} is empty use "index" if {id} has value place in the id parameter. for every url that has only the controller name, the request should go the index action

routes.MapRoute(
   name: "Default",
   url: "{controller}/{action}/{id}",
   defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional     }

);

Upvotes: 3

Barr J
Barr J

Reputation: 10927

This is because your MapRoute is incorrect you're MapRoute is having a default controller of Home:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

change it to:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "case-studies", action = "Index", id = UrlParameter.Optional }
);

Upvotes: 1

Related Questions