Using similar controllers in areas in Asp.Net Mvc

I am developing a sports website with Asp.Net Mvc 4 .

The site is designed to simultaneously show only sports data.

Sports have similar data, but they also have different data.

The site supports many sports, so I do not want to use common controllers / views, sharing sports with if statements.

I tried the following:

I created an area for every sport. I described the controllers in the field related to this sport.

For example, the name of the controller and region is indicated in Route, firstly, it will search in the region, if it is not there, it will search by default ( /Controllers ).

Because the controllers use the same names, the Mvc DefaultControllerfactory throws a "Undefined controller name exception." Im's first search area, if it cannot be found, then Im searches by default by writing its own factory controller. You can reach the project using this link.

In this case, my biggest flaw; without specifying a namespace in the route, which does the same in views. Thus, it will search in the area, if it is not found, then it will search by default. Since the project is supported by topic, I use my own razor viewer mechanism, not the default razor viewer. This can be achieved using this link.

 base.AreaViewLocationFormats = new[] { _themeService.Current.BasePath + "/Views/Areas/{2}/{1}/{0}.cshtml", _themeService.Current.BasePath + "/Views/{1}/{0}.cshtml", _themeService.Current.BasePath + "/Views/Shared/{0}.cshtml","~/Themes/Default/Views/{1}/{0}.cshtml" }; 

I updated the AreaViewLocationFormats AreaViewLocationFormats object like this, but no matter what I specify the area in the route, it searches for ViewLocationFormats instead of AreaViewLocationFormats if I don't specify a namespace.

In this case, how do I separate the sport?

+4
source share
1 answer

What I did in a similar scenario was to create a basic generator controller:

 public abstract class BaseController<TModel> : Controller where TModel : class, new() { // Controller Actions to be shared by all the controllers that inherit form this one... } 

And then your controllers will be like this:

 public class TennisController : BaseController<Tennis> { } 
+1
source

Source: https://habr.com/ru/post/1494471/


All Articles