Controller Class without Controller at the end of the name? - asp.net

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.

Related

Where to put AuthorizeAttribute extended class in ASP.Net authentication?

When implementing authorization for ASP.NET, where should I put AuthorizeAttribute implemented class?
In my project, I have created a class called BasicHttpAuthorizedAttribute which implements System.Web.Http.AuthorizeAttribute class and I have overridden the methods I want.
I have registered this BasicHttpAuthorizedAttribute class as a filter.
My problem is even though I do not mention the [Authorized] attribute on top of controller method, BasicHttpAuthorizedAttribute class's OnAuthorization() method gets called.
That should not be like that, right? It should only be called if you have mentioned [Authorized] attribute on top of controller method. Am I right?
What am I doing wrong here? (My project is a ASP.Net web api project and I am using System.Web.Http.AuthorizeAttribute class)
Basically, it goes into the OnAuthorization() event each time because you've registered it as a filter.
This article has a few neat tips and trips on blanket filtering and anonymous exceptions, which is, I think, what you want. It may be for MVC, but the techniques used should apply to most ASP.NET types with a little tweaking.
http://blogs.msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspx
Example from article:
[HttpPost]
[AllowAnonymous]
public ActionResult LogOn(LogOnModel model, string returnUrl)

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

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

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.

asp.net MVC giving a controller the name 'AdminController' makes it not work

I am working on a ASP.NET application that uses ASP.NET MVC.
I tried naming one of my controllers "AdminController" meaning I typed "Admin" in the new controller text box and it filled out the controller part all by itself of course.
This controller never worked until I changed it's name. If I changed the name to anything else it worked with no problems.
I looked inside my Global.asax.cs file where the routes were configured and I found no routes leading to it.
I tryed adding a route to this new controller like this:
routes.MapRoute("Admin", "calcul/SomeAction",
new { controller = "Admin", action = "SomeAction" });
and it worked but then mysite\admin would only get routed to that specific action.
I renamed the controller to AdminSection and it works but I don't understand why it didn't work before.
Does anyone have any idea
You shouldn't need to create an explicit route for your controller if the default route matches it (controller/action/id).
Also, in the new controller text box you would need to type AdminController rather than just Admin.
The MVC framework will look for classes that end with this when looking for possible controllers.
It was me, there was a area named admin that I didn't see. When I excluded it from the project the "adminController" controller started to work

Resources