【发布时间】:2013-10-24 12:53:41
【问题描述】:
我有一个 ASP.NET MVC 4 项目,它有主页,访问者应该选择子主题。所以结构是:
sypalo.com
photo.sypalo.com
seo.sypalo.com
... and so on
我正在使用 AttributeRouting.net nuget 包来处理到子域的路由。所以每个控制器位于单独的区域并具有以下数据注释(本例中为 SEOController):
[RouteArea("SEO", Subdomain = "seo")]
public class SEOController : Controller
{
private IPostRepository PostRepository;
public SEOController()
{ this.PostRepository = new PostRepository(new BlogEntities()); }
public SEOController(IPostRepository PostRepository)
{ this.PostRepository = PostRepository; }
[GET("{page?}")]
public ActionResult Index(int? page)
{ return View(PostRepository.GetPosts("SEO", page ?? 1)); }
[GET("{year}/{month}/{link}")]
public ActionResult Details(int year, int month, string link)
{
Post post = PostRepository.GetPost("SEO", link);
if (post == null) return HttpNotFound();
return View(post.Text.Replace(ViewRes.Main.ReadMore, ""));
}
}
我实现了存储库以减少重复代码的数量,但仍然需要为每个子域设置具有 CRUD 操作的控制器。所有这些都使用单个表,该表具有称为子域的单独字段,并且我在每个控制器中静态传递子域名。
我正在寻找如何创建具有 CRUD 功能的基本控制器类以及在派生类中扩展它的可能性,因为每个子域也将具有各自的操作/视图。 AFAIK 我可以指定 View 或 SharedView 的位置以具有多个控制器使用的单个视图(单个索引/详细信息/编辑视图)并在 ViewBag 中传递页面标题和其他标签以避免为不同的子域维护相同的代码,但我会非常感谢有人提出更好的方法。
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 attributerouting