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
11011
11011
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
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
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,0762513
2,0762513
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
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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