Jailu Lee
Jailu Lee

Reputation: 641

How can I get the ControllerContext in the class library

I'd like to gain access to the ControllerContext in the class library. Is it possible?

Upvotes: 3

Views: 9019

Answers (3)

Bogdan_Ch
Bogdan_Ch

Reputation: 3336

It depends what information you want to get from there. It may be available in other places. For example controller name and action can be retrieved from

HttpContext.Current.Request.RequestContext.RouteData.Values["controller"]
HttpContext.Current.Request.RequestContext.RouteData.Values["action"]

Upvotes: 3

Tim Sullivan
Tim Sullivan

Reputation: 94

Yes, it is possible. You would have to reference the System.Web.Mvc assembly in the class library and then pass the ControllerContext object to the method you are calling.

The question on whether it is recommended depends on what you are doing. Generally it is not recommended to pass MVC base objects to another assembly dealing with Models.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

Is is possible?

No, this is not something that I would recommend you doing. Don't tie your class library to any ASP.NET specific things such as a controller context. Pass whatever information this library needs as arguments.

There are other ways to achieve whatever you are trying to achieve than retrieving a ControllerContext from a class library.

Upvotes: 3

Related Questions