Reputation: 129
I looked up as much info as I could but I guess no one has tried to do exactly what I'm doing here (I think the same thing could be accomplished with an if statement in the view but I'm trying to reduce the amount of copied code that will be required in my project).
So here's the deal, I want to make certain Html.Partial() calls sensitive to alternate Views.
public static MvcHtmlString DedicatedPartial(this HtmlHelper htmlHelper, string partialViewName)
{
string dedicatedView;
if (ViewExists(partialViewName, htmlHelper, out dedicatedView))
{
return htmlHelper.Partial(dedicatedView);
}
return htmlHelper.Partial(partialViewName);
}
Along with,
private static bool ViewExists(string partialViewName, HtmlHelper helper, out string dedicatedView)
{
var urlPattern = partialViewName.Split('/');
var clientCode = UserHelper.GetClientCode();
dedicatedView = string.Format("ViewsDedicated/{0}/{1}", clientCode, urlPattern[0].ToCharArray()[0] == '~'
? urlPattern[2]
: urlPattern[1]);
return ViewEngines.Engines.FindView(helper.ViewContext, dedicatedView, null).View != null;
}
Inside a view I would like to use,
<div>
@Html.DedicatedPartial("~/Views/Something.cshtml")
</div>
Based on the context of the logged in user I'd like to spit out an alternate view (no changes to context) so that essentially everything remains the same except instead of the expected view a different view is used.
<div>
@Html.Partial("~/DedicatedViews/DEDICATED/Something.cshtml")
</div>
It seems to work sometimes when the code is executed (it has yet to enter the branch). However! Sometimes I get an exception when it tries to execute the lower return htmlHelper.Partial(partialViewName);
The Error:
Section or group name 'system.web.webPages.razor' is already defined. Updates to this may only occur at the configuration level where it is defined.
Thanks for the help!
Edit:
Thanks to the advice below I was able to discover that I was indeed going about the solution in a poor way (creating a helper to manage what a ViewEngine can handle much more appropriately). I should add it turns out the error was actually a side-effect of my testing as there was another ViewEngine that was trying to create a separate instance attempting to handle rendering. That interference caused the not very helpful error.
Long story short: Use ViewEngine(s) to manage complex project directory structures.
Upvotes: 1
Views: 82
Reputation: 5109
I'm not sure what you're trying to do, but when you use partial, you don't need to specify the full path. So maybe you want to change your logic to search based on the name only, without the paths and extension.
@Html.DedicatedPartial("Something")
It would probably also be useful to implement a version where you can pass the model to a partial view.
In fact, I think your best bet would be to create your own view engine. If you inherit the default razor engine, you can specify the search paths and prioritize your dedicated views folder. Then just use Html.Partial
Upvotes: 1