Asp.net MVC - Asynch controller get functionality from my base controller - asp.net

I have a base controller which is subclassed from a standard mvc controller. This containers lots of useful methods specific to a Controller.
I now need to have some asych functionality in one of my new controllers
However, to do that you need to create a controller that subclasses AsyncController
But I also want to access functionality in my base controller
Obviously multiple inheritance isn't possible
so how do I get around this?

You could externalize the functionality you are willing to reuse into a service layer, action filter, authorization filter, model binder, ... it will depend on the functionality you are willing to reuse so that you could easily switch the base controller to an async controller and still preserve the functionality. If you want to use async controllers you will need to derive from AsyncController.

You could make your controller class inherit IAsyncManagerContainer and IAsyncController, then implement this functionality yourself, perhaps using the code from MVC source code. You could even encapsulate this in its own class that you delegate the functionality to.

Related

Controller Class without Controller at the end of the name?

For the reasons that are not important to the question, I would like to know how to make my controllers / routing work in ASP.NET MVC5 if my controller class names do not end in Controller as per convention? Do I need to manually register them somewhere?
The Controller suffix is baked into the the ControllerDescriptor and ControllerTypeCache classes making it hard to override. One way that comes to mind is to write a custom controller factory and override the GetControllerType method.

Filtering the output of my ASP.NET MVC views

I want to do some additional processing of the output of all my views before they get sent to the client.
I tried setting the view base class to a custom class where I override Execute, but that doesn't work because Razor will generate its own Execute in the derived class that doesn't call mine.
Is there another MVC-specific way to do it, or my only hope is to resort to the "classic" way of doing it, by setting Response.Filter in Application_BeginRequest in Global.asax?
You should implement IResultFilter. Common way to do it is by deriving from ActionFilterAttribute
void OnResultExecuted(
ResultExecutedContext filterContext
)

Where to place common business logic for all pages in symfony2

I am now working on my first symfony2 project. I have created a service, and I need to call it for every controllers to generate a html which is necessary throughout the all pages in my website.
So I created a BaseController class which extends Symfony\Bundle\FrameworkBundle\Controller\Controller class and tried to place the code in this BaseController class. Now whenever I call from the constructor:
$my_service = $this->get('my_service');
or
$my_service = $this->container->get('my_service');
I got error:
Call to a member function get() on a non-object.
The container object has not been initialized. What is the solution to this problem? How DRY method is followed in symfony2, if I want to place left panel or header in all pages which contains dynamic data?
Thanks in advance.
You shouldn't use the constructor in your controller class, especially when you inherit from Symfony Controller: that way you get the container after the object instantiation (the DIC will call the setContainer method inherited from Symfony's Controller).
In general, for your first experiments, use the services in the action methods; if there is some cross-cutting logic that you need to execute in every request you could consider registering some event listeners (see the "Internals" docs in the Symfony website).
When you get more confidence with the framework you can start thinking about not inheriting Symfony's Controller, registering your controller classes in the DIC and injecting the services that you need manually (eventually implementing some logic in the constructor).
I know this is not the answer you desire, but if you need some html on all pages, I think using a service the way you do is the wrong way.
I guess you know about twig and the possibility to use a layout to place common code. But you can also embed a controller:
{% render "AcmeArticleBundle:Article:recentArticles" %}
Within the recentArticlesAction, you can place your specific code and return a template. By this, you can get custom html into each of your templates! See the symfony docs for more: http://symfony.com/doc/current/book/templating.html#embedding-controllers
Business logic is all the custom code you write for your app that's not specific to the framework (e.g. routing and controllers). Domain classes, Doctrine entities and regular PHP classes that are used as services are good examples of business logic. Ref

Base page in MVC 2

I have just moved over to using ASP.NET MVC 2. In web forms, I normally had a BasePage class that extends from System.Web.UI.Page. And then every page extends from this BasePage. In this BasePage class I have methods that I need. How would I do this in an MVC application?
Any samples would be appreciated.
Thanks.
It is a bit different in MVC. The equivallent would be BaseController although this doesn't correlate exactly to a page in the classic ASP.NET sense. For a start, a controleler doesn't have any markup.
Into a base controller you might inject any model classes that are required by all pages and any common behaviours that have to be executed as part of all Action requests. An example might be some custom checks to go into the Controller OnActionExecuting event...
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onactionexecuting.aspx
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//check the filterContext for a certain condition
if (condition) {
//do something else - redirect to a different route or
//render a different view to to the default
filterContext.Result = new RedirectResult(newUrl);
}
//Otherwise, do nothing, the requested Action will execute as normal...
}
IN MVC there is a greater separation of concerns for rendering the UI, so depending on what the code did in your base page will dictate where it goes in MVC.
If your code generated HTML than you will probably be creating custom HTML helpers and reusable partials views (.ascx). If it handled input data than it will go in a model binder class, and you can create a base model binder for common code. If it talked to your services and domain model than it will go in the controller, and again you can use a base controller. Queries to the persistence layer will go in your model, and reusing code here leads to a much larger discussion of your architecture.
We also moved from base Page classes in ASP.NET and found that a combination of a base controller and a base Model (ViewData) class works well.
So ex Page properties eg: CurrentUser are available from the base Controller and also passed to the base ViewData when its initiated so you can use them on the aspx page.

Additional information in ASP.Net MVC View

I am attempting to implement a custom locale service in an MVC 2 webpage. I have an interface IResourceDictionary that provides a couple of methods for accessing resources by culture. This is because I want to avoid the static classes of .Net resources.
The problem is accessing the chosen IResourceDictionary from the views. I have contemplated using the ViewDataDictionary given, creating a base controller from which all my controllers inherits that adds my IResourceDictionary to the ViewData before each action executes.
Then I could call my resource dictionary this way:
(ViewData["Resources"] as IResourceDictionary).GetEntry(params);
Admittedly, this is extremely verbose and ugly, especially in inline code as we are encouraged to use in MVC. Right now I am leaning towards static class access
ResourceDictionary.GetEntry(params);
because it is slightly more elegant. I also thought about adding it to my typed model for each page, which seems more robust than adding it to the ViewData..
What is the preferred way to access my ResourceDictionary from the views? All my views will be using this dictionary.
HtmlHelper extension, which will allow you to call your method like this:
<%: Html.GetEntry(params) %>
Seems a pretty good solution

Resources