在Asp.Net Mvc中的区域中使用类似的控制器

时间:2022-11-01 04:12:21

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

我正在使用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.

网站支持许多体育,这就是为什么,我不想通过使用if语句分隔运动来使用公共控制器/视图。

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).

例如,在Route中,将声明控制器和区域的名称,首先它将在区域中搜索,如果不存在,将在默认情况下搜索(/ 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

由于控制器共享相同的名称,因此Mvc DefaultControllerfactory会抛出“不明确的控制器名称异常”。首先,我正在搜索区域,如果找不到,那么我正在搜索默认情况下编写自己的控制器工厂。您可以借助此链接访问该项目

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.

我像这样更新了RazorViewEngine的AreaViewLocationFormats的对象,但不管我在路由中的状态区域,如果我没有声明命名空间,它会搜索ViewLocationFormats而不是AreaViewLocationFormats。

In this case, how should I separate sports?

在这种情况下,我应该如何分开运动?

1 个解决方案

#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>
{
}

#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>
{
}