Using Controller.Content Outside MVC Controller C #

I am creating a static class with static methods, helping controllers do their job. When creating the application, I get the following error:

Error 40 ' System.Web.Mvc.Controller.Content(string) ' is unavailable due to the level of protection "

Any idea how to solve this problem?

Notes: This is a C # mvc application

 public static ActionResult GetAlbumJSON(AlbumVO album) { return Controller.Content( JsonConvert.SerializeObject(new { max_car = @ABookClient.maxCharsProjecName, trans_img = @ABookClient.Transparent_Image, show_description = @ABookClient.Show_Product_Description, product_type = "Album", obj = CreateObjAlbumVO(album), }) ); } 
+5
source share
2 answers

The content method is protected internally, so you cannot use it outside the controller. Controller.Content Method . Most likely, your static class violates the SRP principle. Let him do his work (initialization, serialization, ...) and the task of the controller - the controller - returns the result to the client.

 protected internal ContentResult Content(string content) 

It would seem something like:

 public static class MyHelper { public static object GetAlbum(AlbumVO album) { return new { max_car = @ABookClient.maxCharsProjecName, trans_img = @ABookClient.Transparent_Image, show_description = @ABookClient.Show_Product_Description, product_type = "Album", obj = CreateObjAlbumVO(album), }; } } public class AlbumController : Controller { public ActionResult GetAlbums(int id) { var album = Context.GetAlbum(id); var convertedResult = MyHelper.GetAlbum(album); return Json(convertedResult); } } 

I would also suggest taking a look at AutoMapper to create client response objects.

+4
source

I think this is a valid case for the presentation model for the JSON result, since you want to separate the domain model and the data sent back to the client. Using the view model also gives you a good place to put this mapping between the domain model and the view (JSON), so you don't need to delegate a helper class.

 public class AlbumModel { [JsonProperty(PropertyName = "max_car")] public int MaxChars { get; private set; } [JsonProperty(PropertyName = "trans_img")] public string TransparentImage { get; private set; } [JsonProperty(PropertyName = "product_type")] public string ProductType { get; private set; } [JsonProperty(PropertyName = "obj")] public AlbumInfo Object { get; private set; } [JsonProperty(PropertyName = "show_description")] public bool ShowProductDescription { get; private set; } public AlbumModel(AlbumVO album) { MaxChars = album.maxCharsProjecName; TransparentImage = album.Transparent_Image; ShowProductDescription = album.Show_Product_Description; ProductType = "Album"; Object = new AlbumInfo(album); } } 

The AlbumInfo class provides additional mappings for your JSON result, which becomes the "obj" attribute sent back to the client.

 public class AlbumInfo { // ... define properties here public AlbumInfo(AlbumVO album) { // ... map properties here } } 

And your controller will become nice and clean:

 public class AlbumController : Conrtoller { public ActionResult GetAlbums(int id) { var album = Context.GetAlbum(id); var model = new AlbumModel(album); return Json(model); } } 
+1
source

All Articles