4

I'm developing a sport based web site with Asp.Net Mvc 4.

The site is being developed to show just one sport data at a time.

The sports have similar datas in common but also they have different datas.

Site supports many sports that’s why, I do not want to use common controllers/views by seperating sports with if statements.

I tried this one:

I have created an area for each sport. I described the controllers in area which are related to that sport.

For example, in Route, name of the controller and area will be stated, firstly it will be searched in area, if it is not there, it will be searched in default(/Controllers).

Because the controllers share the same names, Mvc DefaultControllerfactory throws "Ambiguous Controller Name Exception". First I’m searching area, if it cannot be found then I’m searching in default by writing my own Controller factory. You can reach the project by the aid of the this link

In this case, my biggest deficiency is; without indicating namespace in route making the same thing in views. So it will search view in area, if it will not be found then it will search in default. Because the project is theme-supported, I use my own themable razor view engine, not default razor view engine. It can be reached by the aid of 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 object of AreaViewLocationFormats of RazorViewEngine like this but regardless of the fact that I state area in route, it searches ViewLocationFormats instead of AreaViewLocationFormats if I do not state namespace.

In this case, how should I separate sports?

7
  • 1
    Why don't you just have a table with different types of sports (e.g. "football", "tennis", etc.). Then your actions could take a parameter of the type of sport and return the appropriate view? Commented Jul 31, 2013 at 10:58
  • I tihnk the exception is thrown by the mvc DefaultControllerFactory when it tries to figure out which controller to instantiate, it may be worth having a look at multi-tenancy with something like autofac code.google.com/p/autofac/wiki/MultitenantIntegration Commented Jul 31, 2013 at 11:03
  • @DanielPowell, Because I wan't to use one Assembly for the Controllers, I need common(/Controllers). Commented Jul 31, 2013 at 11:15
  • Does each sport have its own Model? Commented Jul 31, 2013 at 13:57
  • @ataravati - Yes, it has. Commented Jul 31, 2013 at 14:33

1 Answer 1

1

What I have done in a similar scenario is creating a base generic controller like this:

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>
{
}
Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense to me, base controller would have the fundamental properties for a "Sports" Controller.

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.