Answered by:
ASP.Net Core MVC BaseController

Question
-
User135423268 posted
Good Day I'm trying to create a base controller on my ASP.NET Core Web Application, I'm currently migrating my applications from .Net Framework to .Net Core, but this code on my base control is not working.
protected override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); string GetString = "Test"; Viewbag.GetString = GetString; } }
The reason for this I have to send the "ViewBag.GetString" to the Layout page for my razor codes, I tried to search it to the internet, but all the answers either it's on the .Net or things that is complicated to understand (for me). I hope someone can help me on this
Thanks and regards
Friday, December 4, 2020 3:21 AM
Answers
-
User475983607 posted
How can I create a Base Controller on ASP.Net Core, It is very different what I do in .Net Framework
Most likely you need to rethink the design using ASP.NET Core fundamental constructs.
ASP.NET Core makes use of dependency injection to pass services (types) to controller constructors. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0
Since you are passing the ViewBag in your original example, I think View Components might be another option. https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-5.0
Keep in mind, it is always best to describe the problem you are trying to solve. Doing so helps us to help you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 9, 2020 12:22 PM
All replies
-
User1535942433 posted
Hi amendoza29,
Accroding to your description,could you tell us the details of your errors?Do you have error messages?
By the way,Initialize() is in asp.net mvc not core mvc.
More details,you could refer to below articles:
https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.initialize?view=aspnet-mvc-5.2
Best regards,
Yijing Sun
Monday, December 7, 2020 9:40 AM -
User135423268 posted
Thanks for the response
How can I create a Base Controller on ASP.Net Core, It is very different what I do in .Net Framework
Wednesday, December 9, 2020 5:23 AM -
User475983607 posted
How can I create a Base Controller on ASP.Net Core, It is very different what I do in .Net Framework
Most likely you need to rethink the design using ASP.NET Core fundamental constructs.
ASP.NET Core makes use of dependency injection to pass services (types) to controller constructors. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0
Since you are passing the ViewBag in your original example, I think View Components might be another option. https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-5.0
Keep in mind, it is always best to describe the problem you are trying to solve. Doing so helps us to help you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 9, 2020 12:22 PM