0

At the place I work at, we have a custom security/authentication mechanism for internal web applications using WebForms. Now we are developing in ASP.NET MVC 3 and want to extend the Controller class to support this just like we did to WebForm's Page class. We will then pack this into a custom - shared DLL.

I want to know how safe this is in terms of compatibility with future versions of MVC. If ASP.NET MVC 4.0 comes out next year, our custom controller classes (referencing ASP.NET MVC 3.0 Controller class) will still work in a MVC 4.0 app?

Is there any way to somehow extend a Controller without depending on an specific MVC version?


Not a duplicate of this: Extending Asp.NET MVC3 Controller Class

6
  • yep, you'd have to upgrade your common library for each version of System.Web.Mvc no way around it unless you have no reference to Mvc...which doesn't seem very useful. Commented Jun 14, 2012 at 21:03
  • So I will need to have MySharedLibrary.Mvc3, MySharedLibrary.Mvc4, ... for every Mvc version? Commented Jun 14, 2012 at 21:08
  • that would be one way...is it a problem? What if next version has a new feature you want to take advantage of? Commented Jun 14, 2012 at 21:26
  • Your question doesn't make sense. You always extend specific version of Controller class, currently MVC 3 version. Controller class gives you a lot of extension points, so if you do it right, you won't have any issues with MVC 4 version. You didn't write how you want to extend, so we can't answer if it will be compatible with version 4. Commented Jun 15, 2012 at 0:03
  • @LukLed I want the same DLL (extending the Controller class) to work with any MVC version without recompiling. I thought it wasn't possible and it seems I was right. I know how to extend it in a way that I only need to update the System.Web.Mvc assembly reference and recompiling... Commented Jun 15, 2012 at 0:31

2 Answers 2

1

The problem is to inherit from the "Controller" class you need to first add a reference to Sytem.Web.Mvc which is version specific.

Each version of ASP.NET MVC will have it's own DLL. You won't be able to use your customer DLL designed for ASP.NET MVC3 in your MVC 4 project.

Now, you could create a class that inheirts from Controller and include it IN your ASP.NET MVC project then as your project version changes so will the reference to "Controller" but thats only because your project has the reference to the version.

Sign up to request clarification or add additional context in comments.

2 Comments

I suppose the same goes for ActionFilterAttributes?
For the most part, yes. In order to inheirt from or extend an action filter you would need a reference to the MVC DLLs. While there is a decent amount of code from ASP.NET MVC2 that could be reused in MVC3 with minor changes, they were not directly compatiable.
0

Not quite what you're asking, but if you're using a custom security/authentication mechanism, you're better off overriding the AuthorizeAttribute which controls access to all controllers.

There are also plenty of examples out there for custom authentication too. Deriving from a custom controller can lead to potential security issues down the line where someone forgets to add the base class - it's normal to apply the AuthorizeAttribute to all controllers in the application startup.

For more information on this approach, see here

For non-security related shared methods, you can implement these as extension methods on IController, rather than extending the base class itself, unless you specifically need to override anything in the controller base class.

3 Comments

We did it once already for an app and it turned out well for our needs. We just want to put all we did in a shared DLL for other applications. We also implemented a custom attribute, but we need some shared methods in all controllers.
If you're after shared methods, then perhaps a better way would be to write extension methods against IController. This way, you don't need to derive from a custom controller either, but your shared methods will still be available.
Wouldn't I have to import "System.Web.Mvc" for this? (thus having version dependency)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.