Custom tokens in view engine?
In ASP.NET MVC 2 project, how might I go about writing a custom view engine that allows custom tokens to be used when searching for views?
Specifically, I'm trying to achieve this:
In PagesController:
public ActionResult ViewPage(string folder, string page)
{
return View(folder, page)开发者_运维问答;
}
I want the view engine to search for the view in the directory: /Views/Pages/[folder]/
How might I achieve this without knowing the folder names ahead of time? Ideally, this customized view engine would only be used for this single controller.
You don't need to implement your own viewengine to solve this issue. You can simply supply the path to the view you want. Something like this:
return View("~/Views/Pages/FolderName/ViewName.aspx");
You example could look something like this:
public ActionResult ViewPage(string folder, string page) {
return View(string.Format("~/Views/Pages/{0}/{1}.aspx", folder, page));
}
If these values change by request (...it looks like that) then you need to overwrite CreateView. I have not done it myself but in one question on SO somebody said its possible:
Localization with separate Language folders within Views
精彩评论