Common Index Action for all controller
up vote
0
down vote
favorite
Currently to return view, I have to write one method in all the controllers. That is "Index" action method. It has nothing to do except returning their respective views.
So is it possible to make them common?
I have inherited one common basecontroller which is inherited from apicontroller. So is there a way that I write base index method. and i can override it as well if needed?
asp.net asp.net-mvc asp.net-core asp.net-core-mvc asp.net-mvc-routing
add a comment |
up vote
0
down vote
favorite
Currently to return view, I have to write one method in all the controllers. That is "Index" action method. It has nothing to do except returning their respective views.
So is it possible to make them common?
I have inherited one common basecontroller which is inherited from apicontroller. So is there a way that I write base index method. and i can override it as well if needed?
asp.net asp.net-mvc asp.net-core asp.net-core-mvc asp.net-mvc-routing
1
I would go for the KISS-principle. Just copy the code. It's more obvious. You already noticed the drawback: AmbiguousActionException
– Ruard van Elburg
Nov 11 at 0:01
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Currently to return view, I have to write one method in all the controllers. That is "Index" action method. It has nothing to do except returning their respective views.
So is it possible to make them common?
I have inherited one common basecontroller which is inherited from apicontroller. So is there a way that I write base index method. and i can override it as well if needed?
asp.net asp.net-mvc asp.net-core asp.net-core-mvc asp.net-mvc-routing
Currently to return view, I have to write one method in all the controllers. That is "Index" action method. It has nothing to do except returning their respective views.
So is it possible to make them common?
I have inherited one common basecontroller which is inherited from apicontroller. So is there a way that I write base index method. and i can override it as well if needed?
asp.net asp.net-mvc asp.net-core asp.net-core-mvc asp.net-mvc-routing
asp.net asp.net-mvc asp.net-core asp.net-core-mvc asp.net-mvc-routing
asked Nov 10 at 21:01
Anonymous Creator
12211
12211
1
I would go for the KISS-principle. Just copy the code. It's more obvious. You already noticed the drawback: AmbiguousActionException
– Ruard van Elburg
Nov 11 at 0:01
add a comment |
1
I would go for the KISS-principle. Just copy the code. It's more obvious. You already noticed the drawback: AmbiguousActionException
– Ruard van Elburg
Nov 11 at 0:01
1
1
I would go for the KISS-principle. Just copy the code. It's more obvious. You already noticed the drawback: AmbiguousActionException
– Ruard van Elburg
Nov 11 at 0:01
I would go for the KISS-principle. Just copy the code. It's more obvious. You already noticed the drawback: AmbiguousActionException
– Ruard van Elburg
Nov 11 at 0:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You can create a BaseController class like this:
public class BaseController : Controller
public virtual IActionResult Index()
return View();
Then inherit your controller clasess from base controller:
public class CustomersController : BaseController
You can override the Index method like this:
public class HomeController : BaseController
public override IActionResult Index()
return View("About");
that resulted into following error: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: MyAppNamespace.Controllers.HomeController.Index (MyAppName) MyAppNamespace.Controllers.HomeController.Index (MyAppName)
– Anonymous Creator
Nov 10 at 21:47
You have to either remove Index method from your HomeController class if you are inheriting from base controller class or you have to use the override keyword with Index method
– Mohsin Mehmood
Nov 10 at 21:49
I removed from homecontroller class. still it gives me this error.
– Anonymous Creator
Nov 10 at 21:55
Are you using MVC areas in your application?
– Mohsin Mehmood
Nov 10 at 21:58
1
The name BaseController is quite confusing: docs.microsoft.com/nl-nl/aspnet/core/web-api/…
– Ruard van Elburg
Nov 10 at 23:57
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can create a BaseController class like this:
public class BaseController : Controller
public virtual IActionResult Index()
return View();
Then inherit your controller clasess from base controller:
public class CustomersController : BaseController
You can override the Index method like this:
public class HomeController : BaseController
public override IActionResult Index()
return View("About");
that resulted into following error: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: MyAppNamespace.Controllers.HomeController.Index (MyAppName) MyAppNamespace.Controllers.HomeController.Index (MyAppName)
– Anonymous Creator
Nov 10 at 21:47
You have to either remove Index method from your HomeController class if you are inheriting from base controller class or you have to use the override keyword with Index method
– Mohsin Mehmood
Nov 10 at 21:49
I removed from homecontroller class. still it gives me this error.
– Anonymous Creator
Nov 10 at 21:55
Are you using MVC areas in your application?
– Mohsin Mehmood
Nov 10 at 21:58
1
The name BaseController is quite confusing: docs.microsoft.com/nl-nl/aspnet/core/web-api/…
– Ruard van Elburg
Nov 10 at 23:57
|
show 1 more comment
up vote
1
down vote
accepted
You can create a BaseController class like this:
public class BaseController : Controller
public virtual IActionResult Index()
return View();
Then inherit your controller clasess from base controller:
public class CustomersController : BaseController
You can override the Index method like this:
public class HomeController : BaseController
public override IActionResult Index()
return View("About");
that resulted into following error: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: MyAppNamespace.Controllers.HomeController.Index (MyAppName) MyAppNamespace.Controllers.HomeController.Index (MyAppName)
– Anonymous Creator
Nov 10 at 21:47
You have to either remove Index method from your HomeController class if you are inheriting from base controller class or you have to use the override keyword with Index method
– Mohsin Mehmood
Nov 10 at 21:49
I removed from homecontroller class. still it gives me this error.
– Anonymous Creator
Nov 10 at 21:55
Are you using MVC areas in your application?
– Mohsin Mehmood
Nov 10 at 21:58
1
The name BaseController is quite confusing: docs.microsoft.com/nl-nl/aspnet/core/web-api/…
– Ruard van Elburg
Nov 10 at 23:57
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can create a BaseController class like this:
public class BaseController : Controller
public virtual IActionResult Index()
return View();
Then inherit your controller clasess from base controller:
public class CustomersController : BaseController
You can override the Index method like this:
public class HomeController : BaseController
public override IActionResult Index()
return View("About");
You can create a BaseController class like this:
public class BaseController : Controller
public virtual IActionResult Index()
return View();
Then inherit your controller clasess from base controller:
public class CustomersController : BaseController
You can override the Index method like this:
public class HomeController : BaseController
public override IActionResult Index()
return View("About");
answered Nov 10 at 21:33
Mohsin Mehmood
2,0412513
2,0412513
that resulted into following error: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: MyAppNamespace.Controllers.HomeController.Index (MyAppName) MyAppNamespace.Controllers.HomeController.Index (MyAppName)
– Anonymous Creator
Nov 10 at 21:47
You have to either remove Index method from your HomeController class if you are inheriting from base controller class or you have to use the override keyword with Index method
– Mohsin Mehmood
Nov 10 at 21:49
I removed from homecontroller class. still it gives me this error.
– Anonymous Creator
Nov 10 at 21:55
Are you using MVC areas in your application?
– Mohsin Mehmood
Nov 10 at 21:58
1
The name BaseController is quite confusing: docs.microsoft.com/nl-nl/aspnet/core/web-api/…
– Ruard van Elburg
Nov 10 at 23:57
|
show 1 more comment
that resulted into following error: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: MyAppNamespace.Controllers.HomeController.Index (MyAppName) MyAppNamespace.Controllers.HomeController.Index (MyAppName)
– Anonymous Creator
Nov 10 at 21:47
You have to either remove Index method from your HomeController class if you are inheriting from base controller class or you have to use the override keyword with Index method
– Mohsin Mehmood
Nov 10 at 21:49
I removed from homecontroller class. still it gives me this error.
– Anonymous Creator
Nov 10 at 21:55
Are you using MVC areas in your application?
– Mohsin Mehmood
Nov 10 at 21:58
1
The name BaseController is quite confusing: docs.microsoft.com/nl-nl/aspnet/core/web-api/…
– Ruard van Elburg
Nov 10 at 23:57
that resulted into following error: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: MyAppNamespace.Controllers.HomeController.Index (MyAppName) MyAppNamespace.Controllers.HomeController.Index (MyAppName)
– Anonymous Creator
Nov 10 at 21:47
that resulted into following error: AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied: MyAppNamespace.Controllers.HomeController.Index (MyAppName) MyAppNamespace.Controllers.HomeController.Index (MyAppName)
– Anonymous Creator
Nov 10 at 21:47
You have to either remove Index method from your HomeController class if you are inheriting from base controller class or you have to use the override keyword with Index method
– Mohsin Mehmood
Nov 10 at 21:49
You have to either remove Index method from your HomeController class if you are inheriting from base controller class or you have to use the override keyword with Index method
– Mohsin Mehmood
Nov 10 at 21:49
I removed from homecontroller class. still it gives me this error.
– Anonymous Creator
Nov 10 at 21:55
I removed from homecontroller class. still it gives me this error.
– Anonymous Creator
Nov 10 at 21:55
Are you using MVC areas in your application?
– Mohsin Mehmood
Nov 10 at 21:58
Are you using MVC areas in your application?
– Mohsin Mehmood
Nov 10 at 21:58
1
1
The name BaseController is quite confusing: docs.microsoft.com/nl-nl/aspnet/core/web-api/…
– Ruard van Elburg
Nov 10 at 23:57
The name BaseController is quite confusing: docs.microsoft.com/nl-nl/aspnet/core/web-api/…
– Ruard van Elburg
Nov 10 at 23:57
|
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243368%2fcommon-index-action-for-all-controller%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I would go for the KISS-principle. Just copy the code. It's more obvious. You already noticed the drawback: AmbiguousActionException
– Ruard van Elburg
Nov 11 at 0:01