简单的ASP.NET MVC的看法,并没有写控制器 [英] Simple ASP.NET MVC views without writing a controller
问题描述
我们正在建立一个网站,将有很小的code,它大多只是将是一堆静态页面担任了。我知道,随着时间的推移,这将改变,我们会想更多的动态信息交换,所以我决定继续前进,建立使用ASP.NET MVC2和星火视图引擎的Web应用程序。将有一对夫妇的控制器,将不得不做实际的工作(如在/产品面积),但大部分将是静态的。
We're building a site that will have very minimal code, it's mostly just going to be a bunch of static pages served up. I know over time that will change and we'll want to swap in more dynamic information, so I've decided to go ahead and build a web application using ASP.NET MVC2 and the Spark view engine. There will be a couple of controllers that will have to do actual work (like in the /products area), but most of it will be static.
我希望我的设计人员能够建立和修改网站,而不必问我每次决定添加或移动网页的时间写一个新的控制器或路线。所以,如果他想添加一个http://mysite.com/News页面,他可以只创建视图下一个新闻文件夹,在其中把一个index.spark页。那么,如果他决定他想要一个/新闻/社区页面后,他可以在该文件夹中删除一个文件community.spark,并有它的工作。
I want my designer to be able to build and modify the site without having to ask me to write a new controller or route every time they decide to add or move a page. So if he wants to add a "http://mysite.com/News" page he can just create a "News" folder under Views and put an index.spark page within it. Then later if he decides he wants a /News/Community page, he can drop a community.spark file within that folder and have it work.
我能够有一个观点没有通过我的控制器覆盖HandleUnknownAction具体行动,但我还是要为每一个这些文件夹的控制器。看来愚蠢必须添加一个空的控制器,并重新编译每次他们决定到一个区域添加到站点的时间。
I'm able to have a view without a specific action by making my controllers override HandleUnknownAction, but I still have to create a controller for each of these folders. It seems silly to have to add an empty controller and recompile every time they decide to add an area to the site.
有什么办法使这个更容易,所以我只需要编写一个控制器和重新编译,如果有做实际的逻辑?一些大师控制器将处理在没有特定的控制器中定义的任何要求吗?
Is there any way to make this easier, so I only have to write a controller and recompile if there's actual logic to be done? Some sort of "master" controller that will handle any requests where there was no specific controller defined?
推荐答案
您必须编写实际控制人/操作的路由映射,并确保默认的索引作为一个动作和id为包罗万象,这将做到这一点!
You will have to write a route mapping for actual controller/actions and make sure the default has index as an action and the id is "catchall" and this will do it!
public class MvcApplication : System.Web.HttpApplication {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "catchall" } // Parameter defaults
);
}
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new CatchallControllerFactory());
}
}
public class CatchallController : Controller
{
public string PageName { get; set; }
//
// GET: /Catchall/
public ActionResult Index()
{
return View(PageName);
}
}
public class CatchallControllerFactory : IControllerFactory {
#region IControllerFactory Members
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) {
if (requestContext.RouteData.Values["controller"].ToString() == "catchall") {
DefaultControllerFactory factory = new DefaultControllerFactory();
return factory.CreateController(requestContext, controllerName);
}
else {
CatchallController controller = new CatchallController();
controller.PageName = requestContext.RouteData.Values["action"].ToString();
return controller;
}
}
public void ReleaseController(IController controller) {
if (controller is IDisposable)
((IDisposable)controller).Dispose();
}
#endregion
}
这篇关于简单的ASP.NET MVC的看法,并没有写控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!