1

My MVC app will have a lot of views (approx 20 to 30). Currently they are all in my HomeController but it has gotten very long:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
        return View();
    }

    /* *SNIP* 29 actions later... */
}

What is the best way to deal with static content pages in MVC 4? Is it best to put them in the HomeController, or have a separate controller related to each information area?

I really want to avoid using a CMS as MVC is great for all my other requirements.

4

1 回答 1

1

如果您没有使用模型或视图包将任何数据传递到视图中,那么为什么不只使用一个索引操作并使用路径中的路径参数来确定要使用哪个视图呢?

所以像这样

控制器

public class HomeController : Controller
{
    public ActionResult Index(string path)
    {
        return View(path);
    }    
}

路线

routes.MapRoute(
    name: "Default", // Route name
    url: "{controller}/{action}/{*path}" // URL with parameters
);

或者,如果您不想让 url 一遍又一遍地包含 home/index,那么就这样做

routes.MapRoute(
    name:"Catchall",
    url: "{path}",
    defaults: new { controller = "Home", action = "Index", path = "DefaultView" }
);
于 2013-06-02T21:53:48.723 回答